Skip to content Skip to sidebar Skip to footer

Variable Between Php And Javascript

code:

Solution 1:

Try

var json = {<?phpecho$json; ?>};

or maybe

var json = eval("{<?phpecho$json; ?>}");

Be sure your code is interpreted as php by the server. The file extension '.js' is usually not configured in this way.

If you want to embed your php file as a js, you'd better to rename it as .php and add the required header like that :

<?php
header("Content-type","text/javascript");
?>var json = {<?phpecho$json; ?>};

Solution 2:

You could try:-

var json = <?echo$json?>;

You cannot execute php code inside the Javascript. Instead you must get php to write the relevent javascript code;

Solution 3:

You could do it other way, that is, put your javascript inside php:

echo "<script>
var json = $json; 
document.write(json.length);
alert('half done');

for(var x=0; x < json.length; x++) {
    document.write(\"<li>\"+ x + \"\" + json[x]+ \"</li>\");
}
</script>";

Post a Comment for "Variable Between Php And Javascript"