so I am a total php/javascript noob.
I’m trying load a full php array into a javascript array.
I wrote this for Javascript:
var names = new Array();
for(var i = 0; i < 48; i++) {
names[i] = "<?php echo giveJS() ?>";
}
And this for php
static $counter = 0;
function giveJS() {
global $names;
global $counter;
$counter++;
return $names[$counter];
}
I already checked if the php array is correctly filled with data.
When I write a output line in javascript, like
document.write(names[10]);
it only gives me the first entry in the php array so it seems like everytime the for loop is repeated its initialising the php file from scratch and so setting the counter to 0.
How can i fix that?
In php
The above code will then output a script tag contain a varible called myarray, then contents of which will be JSON specifying the above array (json_encode formats the array in to javascript internal syntax- the array will probably look like [‘cat’,’dog’,’bat] )
You can then get values from the JavaScript array as so: