Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  79] [ 1]  / answers: 1 / hits: 21998  / 14 Years ago, tue, july 20, 2010, 12:00:00

suppose i have an javascript array userName.I want to load it from a database table named user.



Can anyone help with idea or sample code?



Thanks


More From » php

 Answers
10

Use ajax and php as an intermediate :



  1. Define an empty JS array :


    var myarray = new Array();



  2. use ajax to call your php script, javascript will eval the output by PHP (note this uses prototype library):


     new Ajax.Request('myfile.php', {

    onSuccess : function(xmlHTTP) {

    eval(mlHTTP.responseText);
    }

    });


  3. Write your PHP code to grab data and print JS code (it must be valid javscript !) :


    $i=0; $q=mysql_query('select ..');

    while($row=mysql_fetch_array($q)){

    echo "myarray[".$i."]='".$row['data']."';";

    $i++;
    }


  4. You can check that your Js array contains data :


    alert(myarray.length); 



hope this helps.


[#96170] Saturday, July 17, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
destiney

Total Points: 175
Total Questions: 96
Total Answers: 93

Location: Zambia
Member since Sat, Nov 6, 2021
3 Years ago
;