简体   繁体   中英

javascript - variable values from mysql database

In my site, I have a js file that performs some calculations. Currently I have my variables defined in another js file as global variables.

I would like to make them dynamical. By this I mean I want them to pull values from mysql database. I know that this is possible with ajax:

EXAMPLE:

$.ajax({
    url: 'call.php',
    dataType: 'json'
}).done(function (data) {
    var tag_name = data[0];
    var client_id = data[1];
});

However, this is suitable for few values ( lets say 2 to 10). In my case there are many variables (circa 200). I think that it is not so good idea to make array of size 200 or more.

If I am wrong correct me please. Or if there is better solution to this, please inspire me :).

Thank you

If you have over 200 variables like you wrote here, I think you are organizing the code wrong. You should organize the code into objects that represent an object mapping of your database and call php functions according to your needs.

In call.php use

print json_encode($data_array);
exit();

to encode your array. You already have dataType: 'json' in your javascript $.ajax function. You will NOT need to write var client_id = data[1]; Your data will be accesible through data.user_id, data.tag_name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM