This question already has an answer here:
- to Global Variables inside functions 2 answers
I want to pass "$var_external" to the Javascript "SHOW".
HERE I call the function:
<?php
include("test23.php");
$call_function = test23();
?>
THIS is the function test23.php:
<?php
function test23() {
$var_external = 9999; // This doesn't work
print("<a href=\"dummy\" id=\"SHOW\">Activate script</a>");
}
?>
<script type="text/javascript">
$( function () {
$("a#SHOW").toggle( function() {
var var_external = <?php echo $var_external; ?>;
alert(var_external);
},
function () {
return false;
});
});
If you click on "Activate script" the $var_external should be passed to Javascript.
When $var_external is set inside the PHP-function it doesn't work.
When I put $var_external outside the PHP-function it works.
Why doen't it work inside the PHP-function?
json_encode
to properly encode the variable, otherwise you're not safe. – Marcel Korpel Nov 9 '13 at 19:54