Mohammad
Mohammad

Reputation: 3547

mysql query to js...and storing the result in var

I am getting data from db using mysql query. This data will be used in js functions the following way:

page of db query :

<?php
   $server   = "localhost"; // MySql host name
   $username = "root"; // MySQL username
   $password = ""; // MySQL password
   $dbname   = "admins"; // MySQL database name
   $table    = "usersvisit"; // MySql table name
   $db=mysql_connect($server,$username,$password) or die(mysql_error());
   mysql_select_db($dbname) or die(mysql_error());
   $results=mysql_query("SELECT * FROM $table LIMIT 30") or die(mysql_error());
   $data="[";
   while($row=mysql_fetch_assoc($results))
   {
      $data.= "'".$row['idUser']."',";
   }
   $data.="];";
   echo $data;
?>

Then in the HTML page I want to declare the retrieved data to a variable called origins, so the var origins must store $data to use it later in the other js functions:

function createRequest()
{
    var xmlhttp = false;
    try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try {xmlhttp = new   ActiveXObject("Microsoft.XMLHTTP");}catch(E){xmlhttp = false;}}
    if(!xmlhttp && typeof XMLHttpRequest!='undefined'){try{xmlhttp = new XMLHttpRequest();}catch(e){xmlhttp=false;}}
    if(!xmlhttp && window.createRequest){try{xmlhttp = window.createRequest();}catch(e){xmlhttp=false;}}
    return xmlhttp;
}
window.onload=createRequest;

// Make an xmlHttpRequest to query_database.php
function request_database_query()
{
    var url = "query_database.php";
    var con=createRequest();
    con.open("GET",url,true);
    con.onreadystatechange=function()
    {
       if(con.readyState==4 && con.status==200)
       {
          document.getElementById("div2").innerHTML=con.responseText;
       }
    }
    con.send(null);
}
window.onload=request_database_query;

 var origins= ... // in this var i want to store the data to be as the followling
/*var origins = [
  'Euston',
  'Kings Cross',
  'Liverpool St',
  'Paddington',
  'St. Pancras',
  'Victoria',
  'Waterloo'
];*/

Any suggestions?

Upvotes: 0

Views: 2465

Answers (3)

A S M.Abdur Rab
A S M.Abdur Rab

Reputation: 46

Use exit; after echo $data; in your php code.

And then update your JS function: request_database_query() as follows:

function request_database_query()
{
    var origins = null;   
    var url = "index.php";
    var con=createRequest();
    con.open("GET",url,true);
    con.onreadystatechange=function()
    {
        if(con.readyState==4 && con.status==200)
        {     
            document.getElementById("div2").innerHTML=con.responseText;
            origins = con.responseText;
        }
    }
    con.send(null);

    if(origins != null){
        // call your other JS functions here or do your JS works here
    } 
 }
 window.onload=request_database_query;

Remove the var origins declaration line after window.onload=request_database_query;

Upvotes: 0

WouterH
WouterH

Reputation: 1356

You can store the result of json_encode on an array in PHP in a var value in javascript. In your case you could change $data to an array, and store each $row['idUser'] in there, then json_encode it and pass it to your html page.

An example as follows:

$results=mysql_query("SELECT * FROM $table LIMIT 30") or die(mysql_error());
$data=array();
while($row=mysql_fetch_assoc($results))
{
    $data[] = $row['idUser'];
}

echo "var origins = " . json_encode($data) . ";";

And then in javascript

alert(origins);
for(var i = 0; i < origins.length; i++) {
   alert(origins[i]);
}

Upvotes: 1

Woxxy
Woxxy

Reputation: 1298

Just json_encode the data:

var origins = <?php echo json_encode($data) ?>

Where $data is the PHP array of the data you wish to print.

Upvotes: 0

Related Questions