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: />
-
Define an empty JS array:
var myarray = new array ();- Li>
- Request ('myfile.php', {onSuccess: function (xmlHTTP) {eval (mlHTTP.responseText);}});
-
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 ++; }You can see that the data in your JS array is:
warning (myarray.length);
Hope it helps.
Comments
Post a Comment