[英]How to pass array from php to ajax success function?
I am developing a web app. 我正在开发一个Web应用程序。 I am retrieving data from the database and storing it to an array using the $row = mysqli_fetch_array()
. 我正在从数据库中检索数据,并使用$row = mysqli_fetch_array()
将其存储到数组中。 The problem is, that this array is supposed to go to ajax
using echo json_encode($array)
but I don't know how to receive it from ajax success function, by the way, I am using the jQuery.parseJSON()
. 问题是,该数组应该使用echo json_encode($array)
转到ajax
但是我不知道如何从ajax成功函数接收它,顺便说一句,我正在使用jQuery.parseJSON()
。 After that, I am going to append the values from that array to an <option>
element. 之后,我将把该数组的值附加到<option>
元素。
get_AssetModel.php: get_AssetModel.php:
$sqlGetAssetModels = "select * from tblbrands where brand_name like '$phpGetAssetModels'";
//$array = array();
$models = array();
if ($result = mysqli_query($cn, $sqlGetAssetModels)) {
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_array($result);
$models[] = $row[3];
}
} else {
}
echo json_encode($models);
javascript file who's gonna receive the array: 谁会收到数组的javascript文件:
$.ajax({
url: "inventory/get_AssetModel.php",
type: "POST",
data: chAssBrand,
success: function(data, textStatus, jqXHR){
var data = jQuery.parseJSON(data);
var node = document.createElement("option");
var textnode = document.createTextNode(data.0);
node.appendChild(textnode);
document.getElementById("txtInvModel").appendChild(node);
},
error: function(jqXHR, textStatus, errorThrown){
swal("Error!", textStatus, "error");
}
});
OR IS THEIR OTHER WAY? 还是其他方式?
I assume your data
may look like the below example: 我认为您的data
可能类似于以下示例:
var data = ["Anthony", "Jhake", "Ravelo", "Antigro"]; data.forEach(function(value, index) { var option = document.createElement("option"); option.text = value; option.value = index; var select = document.getElementById("txtInvModel"); select.appendChild(option); });
<select name="txtInvModel" id="txtInvModel"> <option value="">Select one</option> </select>
Hope this will work for you. 希望这对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.