0

I'm trying to pass multiple arrays in Ajax request response(get), but unfortunately I'm not able to get it done.

This is my php code I'm willing to send into Ajax request response:

echo json_encode($catdata);
echo json_encode($productdata);
echo json_encode($data);

My js ajax call is:

$.ajax({
    type: "post",
    url: "../api/test.php",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
        var j=0;
        $.each(data,function(l,item){
            var arrlength = data[l].countdest;
            while(j<=arrlength)
            {
                (function(j)
                {
                    $('#catbtn').click(function(){
                        if(j<=arrlength)
                        {
                            $('#resultdiv').append('<p name="destinationid">'+data[j].destinationid+' '+data[j].name+'</p>');
                            var a;
                            for(a=0;a<4;a++)
                            {
                                alert(a);
                            }
                            //$('#resultdiv').append('<input type="checkbox" name="destinationid" value="'+data[j].destinationid+'" '+data[j].name+'/>');
                            j++;
                            if(j==arrlength)
                            {
                                $('#catbtn').hide();
                                $('#submit').show();
                            }
                        }
                    });
                }
                (j));
                i
            }
        });
        //alert(arrlength);
    },
});
1
  • Please mark the best answer as accepted in order to help future visitors. Commented Sep 10, 2015 at 17:58

2 Answers 2

2
  var formData = {
                    array1 : yourArray1,
                    array2 : yourArray2,
                    array3 : yourArray3
                };
  $.ajax({
       type:"POST",
       url: "trial2.php",
       data: formData,
       success: function(result) {
           console.debug(result);
       },

Edited , now check

Sign up to request clarification or add additional context in comments.

3 Comments

This gives 'data' not defined :'(
Console says: [array[15],array[64],array[4]] and the arrays passed are: $newarray = json_encode(array($catdata, $productdata, $data)); How to read the data in js?
received those array in your php file like $data = $_post['array1']
0

Try to send them all in one array:

echo json_encode(array($catdata, $productdata, $data));

4 Comments

can you please tell me how to these array contents in javascript? It would be really helpfull.
The object created from this json will be an array with three elements. Each one will be one of arrays
Console says: [array[15],array[64],array[4]] and the arrays passed are: $newarray = json_encode(array($catdata, $productdata, $data)); How to read the data in js?
OK, so $catdata is in data[0], $productdata is in data[1] and $data is in data[2]. In other words data is an array of arrays (data is the object given by jquery)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.