admin管理员组

文章数量:1287828

I did a search but I am still confused as I am really new to php and ajax, so I was hoping someone can help me.

Im am using a php script within some ajax to access a database. I can echo the data to replace an element on the webpage. However I want to receive the data as an array to manipulate again in javaScript.

Here is the php

<?php $q=$_GET["q"];

$con = mysql_connect('server', 'name', 'pass'); if (!$con) //don't connect {    die('Could not connect: ' . mysql_error()); //give error }

mysql_select_db("database", $con); //select the MySQL database

$sql="SELECT * FROM table WHERE field = '".$q."'";

$result = mysql_query($sql); //$result is an array

$response = $result;

echo json_encode($response); 

echo "<table border='1'>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['field1'] . "</td>";
  echo "<td>" . $row['field2'] . "</td>";
  echo "<td>" . $row['field3'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

?>

and he is the ajax/jScript used to call the php script.

function func(var)
{
xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) //ready
        {
            document.getElementById("div2").innerHTML=xmlhttp.responseText;

        }
    }
xmlhttp.open("GET","getTest.php?q=" + var,true);
xmlhttp.send();
}

As you can see it replaces div2 with a table with the info. But how can I instead receive the data as an array in jScript?

Cheers

I did a search but I am still confused as I am really new to php and ajax, so I was hoping someone can help me.

Im am using a php script within some ajax to access a database. I can echo the data to replace an element on the webpage. However I want to receive the data as an array to manipulate again in javaScript.

Here is the php

<?php $q=$_GET["q"];

$con = mysql_connect('server', 'name', 'pass'); if (!$con) //don't connect {    die('Could not connect: ' . mysql_error()); //give error }

mysql_select_db("database", $con); //select the MySQL database

$sql="SELECT * FROM table WHERE field = '".$q."'";

$result = mysql_query($sql); //$result is an array

$response = $result;

echo json_encode($response); 

echo "<table border='1'>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['field1'] . "</td>";
  echo "<td>" . $row['field2'] . "</td>";
  echo "<td>" . $row['field3'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

?>

and he is the ajax/jScript used to call the php script.

function func(var)
{
xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) //ready
        {
            document.getElementById("div2").innerHTML=xmlhttp.responseText;

        }
    }
xmlhttp.open("GET","getTest.php?q=" + var,true);
xmlhttp.send();
}

As you can see it replaces div2 with a table with the info. But how can I instead receive the data as an array in jScript?

Cheers

Share Improve this question asked Mar 16, 2012 at 16:13 myolmyol 9,87823 gold badges96 silver badges158 bronze badges 4
  • 2 $result is NOT an array. It's a mysql result handle. You cannot json_encode that and get useable query results from it in Javascript. – Marc B Commented Mar 16, 2012 at 16:16
  • You must fetch results from $result before you can json_encode() it. – Michael Berkowski Commented Mar 16, 2012 at 16:17
  • 2 You should take ten minutes to learn PDO because your variable $q is inserted into your SQL without being escaped. – tadman Commented Mar 16, 2012 at 19:36
  • Thanks guys. I have now used some PDO and looked a bit more into php – myol Commented Mar 17, 2012 at 10:21
Add a ment  | 

3 Answers 3

Reset to default 3

You should look into JSON if you really got plicated objects

As Marc B says, the result of an SQL query is a result handle that you need to read from in order to then JSON encode:

// run the query
$result = mysql_query("SELECT * FROM table WHERE field = '{$q}'");

// fetch all results into an array
$response = array();
while($row = mysql_fetch_assoc($result)) $response[] = $row;

// save the JSON encoded array
$jsonData = json_encode($response); 

In your script, use something like the following to merge that JSON into the JavaScript:

<script>
  var data = <?= $jsonData ?>;
  console.log(data); // or whatever you need to do with the object
</script>
<script>
var foo = <?php echo json_encode($arr); ?>;
</script>

本文标签: php return array into javascriptStack Overflow