0

I have to display multiple values from AJAX but I am getting only single value.

Concept: The user will select single or multiple check box and according to the checkbox, it will display the image and user_id. I am able to display the image but not able to display the user_id. Would you help me in this?

I just upload only AJAX and PHP code

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();
2
  • 1
    json_encode() takes one Array as input, you are passing two. Save all your output to return in one Array and then json_encode it. Commented Aug 28, 2017 at 7:01
  • And how can I display in AJAX? Commented Aug 28, 2017 at 7:09

3 Answers 3

1

the second argument in function jsonencode is for options according following link:

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

for passing multiple values you can put your variables into an array then put this array in function jsonencode

so you can do something like this in your php code:

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

echo json_encode($myArray);

then you get each value in your response in jQuery code like this:

 var firstValue  = msg.compare_pic
 var secondValue = msg.compare_Id
Sign up to request clarification or add additional context in comments.

Comments

0

You're not putting $compare_Id into an array. You need to create a 2-dimensional array in the 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();

Then access each property in the 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");
    }
});

2 Comments

That's correct.It's working. In case If I need any other field that I have to add the field name in SQL query and this.fieldname can retrieve the value. Right?
Cool!! Thanks for helping me again, I am a big fan of you. Upvote from my side.
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();

This should get you started. The above code is returning single array as JSON. In JavaScript, you can display any required property where needed.

7 Comments

Mr.Bilal, I am getting Id but If I click the first check box then getting the first id if I clicked on second check box then I am getting second Id as well as First id
Because you used append in your code. I have update the code above. Check now.
Or you can just write the changes from the OP code and post some additional explanations
Yes I used append because I have to display all the Id which user selected but not duplicate.
|