[ACCEPTED]-How to pass PHP array parameter to Javascript Function?-server-side-scripting
<input type="submit" value="Test Javascript" onclick='showMovies(<?php echo json_encode($movies); ?>);' />
Notice the json_encode
, which encodes objects or arrays 4 for Javascript (JSON stands for JavaScript 3 Object Notation) and also notice the '
instead 2 of "
, because JSON uses "
.
Although, this soulution 1 would be better:
<script type="text/javascript">
document.getElementByID('submitButton').onclick = function() {
var movies = <?php echo json_encode($movies); ?>;
showMovies(movies);
};
</script>
...
<input type="submit" value="Test JavaScript" id="submitButton">
Try this
PHP Code
<script type="text/javascript" src="javascript.js"> </script>
<?php
$movies = array("Bloodsport", "Kickboxer", "Cyborg", "Timecop", "Universal Soldier", "In Hell", "The Quest");
$mov_str = implode(",", $movies);
?>
<input type="submit" value="Test Javascript" onclick="showMovies('<?php echo $mov_str; ?>');" />
In Javascript File
function showMovies(movies) {
movies = movies.split(",");
alert(movies.length);
return false;
}
Output
7
I 1 hope this will help you.
Encode as JSON before outputting.
<input type="submit" value="Test Javascript" onclick="showMovies(<?php echo json_encode($movies); ?>);" />
0
Your PHP variables only live on the server. They 18 are completely separate from the JavaScript 17 variables on the client. The only mechanism 16 for passing values from the server to the 15 client is through the contents of the web 14 page (or through specially requested behind-the-scenes 13 web content through AJAX).
This means that to 12 make your JavaScript receive PHP values, you 11 have to write JavaScript with those values 10 embedded inside of it. You must mix the 9 PHP with the JavaScript at least a tiny 8 bit to get the stuff that runs on the client 7 to have any data from the server.
This is 6 how all web server-side scripting works 5 in all languages.
JavaScript simply cannot 4 know what goes in your movies variable unless 3 you stuff it full of values, in JavaScript.
I recommend 2 you to @levu's answer to see a good way to get your PHP 1 variable's values into JavaScript.
<?php
$movies = array("Bloodsport", "Kickboxer", "Cyborg", "Timecop", "Universal Soldier", "In Hell", "The Quest");
$str=implode(",",$movies);
?>
<script type="text/javascript" >
var arr=new Array();
var str="<?php echo $str; ?>";
arr=str.split(',');
//alert(arr.toSource());
function showMovies(movies) {
alert(movies.length);
return false;
}
</script>
<input type="submit" value="Test Javascript" onclick="showMovies(arr)" >
Hey Please use above for your desired result. It 1 is tested code.
You can pass array in js by json_encode() php 3 function.. json_encode() will make array 2 into string. you can get array back by saprating 1 that string in js.
You can also parse an array for JavaScript 2 with json_encode()
$array = array('1','a','b');
echo '<script type="text/javascript">';
echo 'var x = ' . json_encode($array) . ';';
echo '</script>';
This also works for associative 1 arrays:
$array = array("x" => '1',"y" => 'a',"z" => 'b');
echo '<script type="text/javascript">';
echo 'var x = ' . json_encode($array) . ';';
echo '</script>';
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.