tags:

views:

48

answers:

4

Hi there, 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?

A: 

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

Liam Bailey
NOw that question is fully visible, if you remove the call to echo, your pHP variable will contain the JS string.
Liam Bailey
+2  A: 

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.

ILMV
remove the braces around echo, its not print.
RobertPitt
A: 

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

reko_t
+1  A: 

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...

Chouchenos
Rishi2686
Rishi2686
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
Chouchenos
and now your previous button : <a href="#" id="previous">Previous</a> with jQuery : $('#previous').click(function(){no -= no; if(no<0){no=0;}else{$.post("b.php", {no: no}, function(msg){$('#id-of-the-target-in-a.php').html(msg);});}});
Chouchenos
sorry but bounced : here is my code snippet for links:
Rishi2686
echo "<a id =\"$p\" class=\"gav_pn\" href=\"javascript:void(0)\" onclick=\"open_page('userinfo.php?pageno1=$p\" style='text-decoration:none'><font-size = '14'><strong>$p</strong></font></a>"." ";
Rishi2686
$p is the variable in forloop, open_page() is ajax function where userinfo.php contains code to query database and get user data. content is the target element printed with <div> tag.
Rishi2686
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\" 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\" style='text-decoration:none'><font-size = '14'><strong>previous</strong></font></a>"." ";
Chouchenos
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...
Chouchenos