Php 在ajax成功函数中返回两个变量
如何在一个函数中同时返回地块号和注释数?我有两个单独的sql字符串返回不同的结果。我希望能够返回apn编号,并将值放入标签中,以及有多少条评论的计数。这可能吗?我该怎么做 Jquery:Php 在ajax成功函数中返回两个变量,php,jquery,ajax,Php,Jquery,Ajax,如何在一个函数中同时返回地块号和注释数?我有两个单独的sql字符串返回不同的结果。我希望能够返回apn编号,并将值放入标签中,以及有多少条评论的计数。这可能吗?我该怎么做 Jquery: $.ajax({ url: "classes/get-apn-count-comments.php?parcel_id=" + parcel_id, type: "GET", data: { parcel_id : parcel_id }, dataType: 'json',
$.ajax({
url: "classes/get-apn-count-comments.php?parcel_id=" + parcel_id,
type: "GET",
data: { parcel_id : parcel_id },
dataType: 'json',
error: function(SMLHttpRequest, textStatus, errorThrown){
alert("An error has occurred making the request: " + errorThrown);
},
success: function(data2){
//do stuff here on success
//$('#ParcelNumber').html(data[0]["apn"]);
$('#ViewComments').val('View ' + data2[0].count + ' Comments');
}
});
PHP:
在php端创建一个数组,用于保存答案1和答案2。然后,json_编码并回显新的多维数组,就像处理旧数组一样 然后在回调的javascript端运行JSON.parse()将数组转换为javascript对象 例如:
if(isset($_GET['parcel_id'])) {
$db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$return = array();
//get the apn based on id
$data = $db->get_results("select apn from parcels where parcel_id=" . $_GET['parcel_id']);
if($data != null){
$return['data_one'] = $data;
}
//count number of comments for the id
$data2 = $db->get_results("select count(*) as count from comments where parcel_id=" .$_GET['parcel_id']);
if($data2 != null){
$return['data_two'] = $data2;
}
echo json_encode($return);
}
js:
我现在得到了“uncaughtsyntaxerror:unexpectedtokeno”,这可能是某种语法错误。可能是由于php错误,导致javascript试图读取的标记出错。因此,它没有得到xyz,而是读取了一条php错误消息,而jquery不知道如何解析它。
if(isset($_GET['parcel_id'])) {
$db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$return = array();
//get the apn based on id
$data = $db->get_results("select apn from parcels where parcel_id=" . $_GET['parcel_id']);
if($data != null){
$return['data_one'] = $data;
}
//count number of comments for the id
$data2 = $db->get_results("select count(*) as count from comments where parcel_id=" .$_GET['parcel_id']);
if($data2 != null){
$return['data_two'] = $data2;
}
echo json_encode($return);
}
success: function(res){
//do stuff here on success
res = JSON.parse(res);
console.log(res);
}