php - Load Javascript array with MYSQL database data -


Assume that I have a JavaScript array "userName". I want to load it from the database table named "user".

Can anyone help with ideas or sample codes?

Thanks

Use AJAX and PHP as an intermediate:

  1. Define an empty JS array:

    var myarray = new array ();

  2. Li>
  3. Request ('myfile.php', {onSuccess: function (xmlHTTP) {eval (mlHTTP.responseText);}});
  4. To get the data, type your PHP code and print the JS code (this should be a valid JavaScript!):

      $ I = 0; $ Q = mysql_query ('select ..'); While ($ line = mysql_fetch_array ($ q)) {echo "myarray [". $ I. "] = '". $ Line ['data']. ' ';'; $ I ++; }  
  5. You can see that the data in your JS array is:

      warning (myarray.length);  

Hope it helps.


Comments