0

How can i assign a javascript value to a php variable,

This is what i want to do:

<?php
    $pag = echo "<script language ='javascript'>var pn =                 document.getElementById('t').value; 
            document.write(pn); </script>";


?> 

But getting error as: Parse error: syntax error, unexpected T_ECHO

or is there any other way to do, but i want to assign that valur to php variable. can somebody help here?

4 Answers 4

2

First, you can't give an "echo" to a variable. echo send a string or an information to the browser.

Then, you need to understand that Javascript is interpreted on the browser and PHP on the server.

It means, PHP can send variable to javascript (in an ugly and static way or with Ajax) but Javascript can't, unless you use it to change page sending the variable via GET or via AJAX.

Finally you should tell us what you need that for...

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you Chouchenos, here is the case: Actually I need this for pagination. I am not able to get current page no. On a.php, I wrote for loop to create links (1-10), and on link called ajax function to which i pass url(b.php which gives userdata) and target(div tag to print on a.php), problem is that i don't get clicked page no on a.php to create next & previous links. So i used input tag with hidden field and trying to access it above the for loop with the above code in question.
cont.. I am trying to get this done from last 4 days with each & every way. but no success. @ILMV
I'm more used to jQuery than raw JS but I'll give a shot : if you got your links like this <a href="#" id="1">1</a> <a href="#" id="2">2</a> etc. You can make your ajax request and stock the "current" no like this in jQuery : var no = 0; $('a').click(function(e){no = $(this).attr('id'); e.preventDefault(); $.post("b.php", {no: no}, function(msg){$('#id-of-the-target-in-a.php').html(msg);});}); // This way your variable named "no" contains the current page
Mmmm ok, here's an idea. You change your links like this : echo "<a id =\"$p\" class=\"gav_pn\" href=\"javascript:void(0)\" onclick=\"open_page('userinfo.php?by=$by1&order=$ord&perpage=$rowperpage','content', $p);\" style='text-decoration:none'><font-size = '14'><strong>$p</strong></font></a>"." "; echo "<a id =\"previous\" class=\"gav_pn\" href=\"javascript:void(0)\" onclick=\"open_page('userinfo.php?by=$by1&order=$ord&perpage=$rowperpage','content', 'previous');\" style='text-decoration:none'><font-size = '14'><strong>previous</strong></font></a>"." ";
Then in your a.php's js, just initialize your page number (ie: var no = 0;) and in your function open_page(url, id, number) if number is a number (ie: = $p) then modify your url to add the parameters pageno1=number. If number is equal to the string "previous" then pageno1=no-1; It's roughly explained... I hope you understand the idea...
|
1

Javascript is client side, all PHP code is loaded by the server, before sending to the client. Thus, the only way to access JS variables in PHP is by setting a cookie in js, or by using AJAX

1 Comment

NOw that question is fully visible, if you remove the call to echo, your pHP variable will contain the JS string.
1

If you want to assign the value to a variable and then echo it out, use this code instead:

<?php
    $pag="<script language ='javascript'>var pn = document.getElementById('t').value; document.write(pn); </script>";
    echo($pag);

?> 

Edit

Looking back over your question it would be good if you could explain exactly what you're trying to achieve.

3 Comments

remove the braces around echo, its not print.
@RobertPitt: You forgot a ";" and a "'" there. (See how useful is needless pedanticism?)
Why does this answer have two upvotes? This is a fundamental impossibility.
0

Remove the echo.. You're assigning to a variable, not outputting anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.