我想将一个json数组返回给调用的$.ajax function
,但我只得到了所需数组的最后一项。也许我没有产生一个数组?
如果我单击id为"btn_getAnswers"的按钮,"$("#btn_getAnswers").click"
将被激发,"DBCOMANSWERS"
的代码将被执行。我希望"DBCOMANSWERS"中的"$result"
是一个数组,用我的MYSQL数据库的值填充。我返回格式化为JSON的"$result"
。返回的结果应附加到id为"output"的段落中。到目前为止,这一切都很好,但除了要返回并附加到段落中的三个字符串之外,现在只附加了一个字符串,即数据库中最后一个捕获的条目。
我真的看不出我必须把一个循环放在哪里才能添加或其他什么。返回的$result可能不是数组,只是数据库的最后一个条目,因为它被覆盖了吗?
Index.html:
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.12.3.js"></script> <!-- Import the jquery extension -->
<script>
$(document).ready(function () {
$("#btn_getQuestion").click(function () {
$.ajax({
type: "POST",
url: "DBCOMQUESTIONS.php?q=" + $("#input").val(),
success: function (result) { //Performs an async AJAX request
if (result) {
$("#output").html(result); //assign the value of the result to the paragraph with the id "output"
}
}
});
});
$("#btn_getAnswers").click(function () {
$.ajax({
type: "POST",
url: "DBCOMANSWERS.php?q=" + $("#input").val(),
success: function (result) { //Performs an async AJAX request
if (result) {
$("#output").append(result);
}
}
});
});
});
</script>
</head>
<body>
<p id="output">This is a paragraph.</p>
<input id="input"/>
<button id="btn_getQuestion">Question</button>
<button id="btn_getAnswers">Answers</button>
</body>
</html>
DBCOMANSWERS.php:
<!DOCTYPE HTML>
<head>
</head>
<body>
<?php
include("connection.php"); //includes mysqli_connent with database
include("ErrorHandler.php"); //includes error handling function
set_error_handler("ErrorHandler"); //set the new error handler
$q = intval($_GET['q']);
$sql="SELECT * FROM tbl_answers WHERE QID ='".$q."'"; //define sql statement
$query = mysqli_query($con,$sql); // get the data from the db
while ($row = $query->fetch_array(MYSQLI_ASSOC)) { // fetches a result row as an associative array
$result = $row['answer'];
}
echo json_encode($result); // return value of $result
mysqli_close($con); // close connection with database
?>
</body>
<html>
你需要做两件事
删除html并添加数组集合。这就是DBCOMANSWERS.php必须看起来像的方式
<?php
include("connection.php"); //includes mysqli_connent with database
include("ErrorHandler.php"); //includes error handling function
set_error_handler("ErrorHandler"); //set the new error handler
$q = intval($_GET['q']);
$sql="SELECT * FROM tbl_answers WHERE QID ='".$q."'"; //define sql statement
$query = mysqli_query($con,$sql); // get the data from the db
$result = [];
while ($row = $query->fetch_array(MYSQLI_ASSOC)) { // fetches a result row as an associative array
$result [] = $row['answer'];
}
mysqli_close($con); // close connection with database
header('Content-Type: application/json');
echo json_encode($result); // return value of $result
?>
然后在你的html中,@madalinivascu建议
success: function(result){ //Performs an async AJAX request
result.forEach(function(i,v){
$("#output").append(v.answer);
})
}}
尝试:删除所有html标记和
include("ErrorHandler.php"); //includes error handling function
set_error_handler("ErrorHandler"); //set the new error handler
从ajaxed php文件中,创建一个结果数组,并将每个结果附加到中
$result = []
while ($row = $query->fetch_array(MYSQLI_ASSOC)) { // fetches a result row as an associative array
$result[] = $row['answer'];
}
header('Content-Type: application/json');//change header to json format
在ajax函数中,您需要执行一个循环:
success: function(result){ //Performs an async AJAX request
result.forEach(function(i,v){
$("#output").append(v.answer);
})
}}
TRY:
$result = []
while ($row = $query->fetch_assoc()) { // fetches a result row as an associative array
$result[] = $row['answer'];
}
参考:
http://php.net/manual/en/mysqli-result.fetch-array.php