javascript php将数组返回到javascript中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9740775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php return array into javascript
提问by myol
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.
我进行了搜索,但我仍然很困惑,因为我对 php 和 ajax 真的很陌生,所以我希望有人能帮助我。
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.
我在某些 ajax 中使用 php 脚本来访问数据库。我可以回显数据以替换网页上的元素。但是我想将数据作为数组接收,以便在 javaScript 中再次操作。
Here is the php
这是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.
而他就是用来调用php脚本的ajax/jScript。
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?
如您所见,它将 div2 替换为包含信息的表格。但是我怎样才能在 jScript 中以数组的形式接收数据呢?
Cheers
干杯
采纳答案by steveukx
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:
正如 Marc B 所说,SQL 查询的结果是您需要读取的结果句柄,然后才能进行 JSON 编码:
// 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:
在您的脚本中,使用以下内容将该 JSON 合并到 JavaScript 中:
<script>
var data = <?= $jsonData ?>;
console.log(data); // or whatever you need to do with the object
</script>
回答by Michael Laffargue
You should look into JSON if you really got complicated objects
如果你真的有复杂的对象,你应该研究 JSON
回答by Sinetheta
<script>
var foo = <?php echo json_encode($arr); ?>;
</script>