Can some one please HELP!!!! How do I pass an array from php to JavaScript?
Thanks...
Can some one please HELP!!!! How do I pass an array from php to JavaScript?
Thanks...
Write it.
echo "soemthing = new Array( (whatever the syntax for specifying a Javascript array is));";
u canot pass array directly from php to javascript.. u have to make a string using special characters and then remove the special characters in javascript...
e.g $str .= "@".$id."##".$name; --- $id is the index and $name is the value
and just build the array in javascript removing "@" using split('@') function and then again use split to remove "##" and form ur new array in javascript..
you can pass the php array to a text field within a form, then you can then use javascript to read the data from the form to the java array
Everybody is right basically...I do it Weedpackets way and simply write it out (using PHP):
$images = array('image1.jpg','image2.jpg','image3.jpg');
foreach ($images as $key => $image) {
$javascript .= "var image[$key] = '$image';\n";
}
echo "<script type='text/javascript'>'$javascript</script>";
To illustrate the concept...and yeah, you may need to run some htmlspecialchars and such on your outputs to make javascript happy.