0

Possible Duplicate:
Pass a PHP string to a Javascript variable (and escape newlines)

Can anyone show me the method to pass php variable into javascript function and also javascript variable into php script.Here is the sample line i want to pass php variable , but when i am doing that function is not getting executed

echo "<td><a href='#' onclick='disp_confirm($row->university)'>Delete</a></td>";

please help me.

Community
  • 1
  • 1
  • vice versa not possible, but php in javascript yes: `disp_confirm("'.$row->university.'")`, if you use single quotes on echo your variables will not get parsed. – Mihai Iorga Nov 21 '12 at 10:02
  • vice versa is possible but far more complex (since the Javascript will only begin executing long after the PHP has ended.) You just need to call a PHP script with AJAX, and forward the variable as a CGI variable. – SF. Nov 21 '12 at 10:05
  • @SF. vice versa in OP's context is not possible. – Mihai Iorga Nov 21 '12 at 10:10

4 Answers4

0

Add braces and will will work:

echo "<td><a href='#' onclick='disp_confirm({$row->university})'>Delete</a></td>";
Adam
  • 1,214
  • 13
  • 23
0

You need to quote the string in the onclick:

echo "<td><a href='#' onclick='disp_confirm(\"{$row->university}\")'>Delete</a></td>";
Larry Williamson
  • 1,149
  • 5
  • 18
0

To print a variable into Javascript you just add curly braces (this only works when you are emitting a double quoted string, so:

 echo "<td><a href='#' onclick='disp_confirm({$row->university})'>Delete</a></td>";

will work and

 echo '<td><a href="#" onclick="disp_confirm({$row->university})">Delete</a></td>';

will not work. The same for {$variables}, {$array['of values']} etc.

(In the above code, $row->university has to be an integer, or you'll get a Javascript error. If so, quote the value with escaped single quote marks)

UPDATE (thanks to Mihai Iorga): you must take into account how the variable will interact with the Javascript code. It is not enough to quote strings, because the variable might itself contain a quote mark. So if your university was "Shi'Kahr University", the HTML would become

onclick='disp_confirm(\'Shi'Kahr University\')'

and the Javascript would be immediately broken. So you also have to use addslashes() to ensure that your string is properly quoted. Since you are writing a string inside a string, you need to call addslashes() twice, to get

onclick='disp_confirm(\'Shi\\\'Kahr University\')'

To pass a Javascript value to PHP script on server side, you can use AJAX (e.g. jQuery library) or you can add the value to the URL and reload the page:

 alert("Submitting " + value);
 location.href = '?variable=' + value;

The script will execute again from the beginning (which may or may not be what you want), and $_GET['variable'] will then contain value. You can distinguish the two cases with an if:

<?php
    if (isset($_GET['variable']))
    {
        // This is the second round
    }
    else
    {
        // This is the first time
    }
    // common code goes here
?>

Finally, there are libraries that allow mixing PHP and Javascript. They basically work as above, but they take care of the heavy lifting, variable passing, escaping, and calling back. See for example XAJAX, but several others exist.

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107
0

You cannot pass javascript variables to PHP, unless you send the information by submitting a form (because JS is in the client side, not in the server) or with AJAX.

In you want to write PHP variables into JS, you have to echo the variable into de HTML/JS code, like this:

<td><a href='#' onclick='disp_confirm(<?php echo $row->university?>)'>Delete</a></td>

or:

function name() {
    var i=<?php echo $data?>;
    ...
}
RainHeart257
  • 305
  • 1
  • 9