How to transfer data from sql to javascript?


I work with this example: But I need get data from MySql DB.

I have 2 main files: 1) map.php (connect to db and show svg map) 2) map.js (show colorized map with static data)

In JS file I see rows:

enter code here
var regions=[
        "region_name": "Lombardia",
        "region_code": "lom",
        "population": 9794525
        "region_name": "Campania",
        "region_code": "cam",
        "population": 5769750
    // etc ...

I need change value of region name to value from db. But I do not how?

In my php main variable are:

echo "Region: ".$row['region_name']."
echo "Code of region: ".$row['region_code']."
echo "Value: ".$row['value']."

First of all your PHP code should connect to the database, extract the data you are interested in and build a json string from it, just like the regions data from your example. Your PHP will return this json. You will probably want to work with json because it is the most ajax/js friendly format.

Your JS code should make a Ajax call to that PHP for retrieving the json formatted data. Assuming you are using jquery, it is something like this:

    url : "map.php",
    dataType : "json",
    done: function(data){
       // the data parameter is the json returned by the php
       // already converted to javascript object...
       // Just like the regions array from your example.

For further information and options about the jquery ajax api, see