Skip to content Skip to sidebar Skip to footer

Can I Move Array From Php To Javascript Retrive From Mysql Database

my php code get_marker_connect2.php

Solution 1:

If you wish to print a complex structure into your page that is worked in a browser, you need a syntax that Javascript can parse, and the serverside language can print.

The neareset possibilities are:

  • XML
  • JSON

I recommend JSON for now.

So with PHP, you write somewhere:

echo "var myTransportedArrayJson = \"" . json_encode($my_array) . "\"";

And then let the Javascript parse your json expression:

var myTransportedArray = JSON.parse(myTransportedArrayJson);
alert(myTransportedArray);   // <<< and here you are.

Of course the snippet must be part of the response, otherwise it won't reach the server. Also, you should embrace the parsing with try .. catch. If you go on and work with even more complex structures, you have to consider escaping certain chars. So you will find now some literature :-)

Post a Comment for "Can I Move Array From Php To Javascript Retrive From Mysql Database"