I am a bit confused here on how I implement an array into JS from PHP….
I’m doing this for testing – eventually I’ll use long polling or websockets if they get highly supported but this is what I have:
$get = mysql_query("SELECT x,y,sid FROM player_town WHERE uid='1'") or die(mysql_error());
$row = mysql_fetch_assoc($get);
$data = json_encode($row);
Further down the script I then put in the head:
<script>var data = Array(<? $data; ?>)</script>
<script type="text/javascript" src="js.js"></script>
But in js.js it says undefined but $data is set. This is the error:
x is not defined
In js.js I did alert(data[x]); and I get undefined.
My json_encode looks like this:
{"x":"283","y":"99","sid":"1"}
Any ideas?
Not sure why you need to wrap the json string in an Array, you could just do
—
To get the value of data in your js, you can either do data.x or data[“x”]