如何在PHP中显示来自AJAX的多个值?

时间:2017-08-28 06:58:08

标签: php jquery json ajax

我必须从AJAX中显示多个值,但我只获得单个值。

概念: 用户将选中单个或多个复选框,并根据复选框显示图像和user_id。我能够显示图像但无法显示user_id。你能帮帮我吗?

我只上传AJAX和PHP代码

AJAX

$.ajax({
    type: "POST",
    url: "includes/compare_process.php", // 
    data:'users='+arr,
    dataType: 'json',
    success: function(msg){
        $("#pics_name").empty();
        $.each(msg, function() {
            $("#pics_name").append("<img src='images/profile/" + this + "' alt='' />");
            $("#pics_Id").append();//I have to display id here
        });
    },
    error: function(){
        alert("failure");
    }
});

PHP

$_SESSION['compare_user']=$_POST['users'];
$sql_compare='SELECT * FROM request WHERE Id IN (' .( is_array( $_SESSION['compare_user'] ) ? implode( ',', $_SESSION['compare_user'] ) : $_SESSION['compare_user'] ).')';
$compare_query=$conn->query($sql_compare);
if ($compare_query->num_rows > 0) {
    while($userdata12=$compare_query->fetch_assoc()){ 
        $compare_pic[]=$userdata12['profile_pic'];
        $compare_Id=$userdata12['Id'];
    }
}
echo json_encode($compare_pic, $compare_Id);
exit();

3 个答案:

答案 0 :(得分:1)

函数jsonencode中的第二个参数是针对以下链接的选项:

http://php.net/manual/en/function.json-encode.php

用于传递多个值,您可以将变量放入数组中,然后将此数组放入函数jsonencode

所以你可以在php代码中做这样的事情:

$myArray = [
     'compare_pic' => $compare_pic,
     'compare_Id'  => $compare_Id
];

echo json_encode($myArray);

然后你在jQuery代码中得到你的响应中的每个值,如下所示:

 var firstValue  = msg.compare_pic
 var secondValue = msg.compare_Id

答案 1 :(得分:0)

$.ajax({
    type: "POST",
    url: "includes/compare_process.php", // 
    data:'users='+arr,
    dataType: 'json',
    success: function(msg){
        $("#pics_name").empty();
        $.each(msg, function (index, value) {
            $("#pics_name").html("<img src='images/profile/" + value.profile_pic + "' alt='' />");
            $("#pics_Id").html(value.Id);
        });
    },
    error: function(){
        alert("failure");
    }
});

PHP

$_SESSION['compare_user']=$_POST['users'];
$sql_compare='SELECT * FROM request WHERE Id IN (' .( is_array( $_SESSION['compare_user'] ) ? implode( ',', $_SESSION['compare_user'] ) : $_SESSION['compare_user'] ).')';
$compare_pic = array();
$compare_query=$conn->query($sql_compare);
if ($compare_query->num_rows > 0) {
     while($userdata12=$compare_query->fetch_assoc()){ 
            $compare_pic[]=$userdata12;
     }
   }
echo json_encode($compare_pic);
exit();

这应该让你开始。上面的代码将单个数组作为JSON返回。在JavaScript中,您可以根据需要显示任何所需的属性。

答案 2 :(得分:0)

您没有将$compare_Id放入数组中。您需要在PHP中创建一个二维数组。

$_SESSION['compare_user']=$_POST['users'];
$sql_compare='SELECT id, profile_pic FROM request WHERE Id IN (' .( is_array( $_SESSION['compare_user'] ) ? implode( ',', $_SESSION['compare_user'] ) : $_SESSION['compare_user'] ).')';
$compare_pic = array();
$compare_query=$conn->query($sql_compare);
if ($compare_query->num_rows > 0) {
     while($userdata12=$compare_query->fetch_assoc()){ 
            $compare_pic[]=$userdata12;
     }
   }
echo json_encode($compare_pic);
exit();

然后访问Javascript中的每个属性。

$.ajax({
    type: "POST",
    url: "includes/compare_process.php", // 
    data:'users='+arr,
    dataType: 'json',
    success: function(msg){
        $("#pics_name,#pics_Id").empty();
        $.each(msg, function() {
            $("#pics_name").append("<img src='images/profile/" + this.profile_pic + "' alt='' />");
            $("#pics_Id").append("<div>" + this.id + "</div>");
        });
    },
    error: function(){
        alert("failure");
    }
});