Using php4 how could I store an array in a hidden div so that jquery can pick it up and then turn it into a json object? Im trying to pass an array of data to gmap so that I can load multiple markers, but cant seem to figure out how to pass a php array to jquery and then turn it into a json object.
I tried using a custom json encode class for php4 and encoding the php array to json, the problem is it spits out a bunch of garbage characters and appears to cause errors in the jquery code. Here is the array Im creating in php:
$map_array[] = array('latitude' => $result_latitude,'longitude' => $result_longitude,'html' => $result_html,'title' => $result_name,'icon' => array('image' => '/pathtoicon' .$mapi .'.png','iconsize' => array(27,27)));
$map_json = $json->encode($map_array);
Then in jquery:
var mapcoords = $('#mapcoord').html();
$('#rmap').gMap(
{
zoom: 10,
markers:$.parseJSON(mapcoords)
}
);
When I do this, I get this error:
“SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data”
This works if I do var mapcoords = $(‘#mapcoord’).text();
The problem is, it strips out the html. So how do I pass the php array to jquery and be able to preserve the html code?
Why don’t you simply create the javascript variable with php and set it there?
For example:
Now the variable is accessible through javascript as an object.
If you can’t do that, you can try to convert the string into an json object using jQuery.parseJSON.
Hope it helps!
Did you try
jQuery.parseJSON(myString)?