I have a list in javascript that is to be changed by the content of a database when the user clicks a link or slides a bar. I currently use a xmlhttp request to fetch a php page which generates the list.
I have a static list working in this form:
var mark_list = [
{ "EntID" : 3, "x" : 100, "y" : 400},
];
I tried to have the php page generate the { “EntID” : 3, “x” : 100, “y” : 400} and set mark_list equal to responseText but I believe that’s just a string. I’m struggling to find a way to get this new list into the variablie.
Does anyone have any suggestions or solutions? Your help would be greatly appreciated.
Kallum
Make sure the browser has a JSON object by including the json script from here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
It will defer to the native browser JSON object if available, and if not, will add it so that this functionality is available in browsers that don’t currently support it.
Then in your code, after you receive the output from the PHP script, run this:
Based on the output from the PHP script, you may need to do this:
This will convert the JSON string the PHP returns into an actual javascript object.
In your PHP code you can do something like this:
Then you can use the
var mark_list = JSON.parse(responseText);option in your JavaScript.