Rajan
Rajan

Reputation: 105

Pass multiple php arrays in ajax request response

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);
    },
});

Upvotes: 0

Views: 1315

Answers (2)

Drop Shadow
Drop Shadow

Reputation: 808

  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

Upvotes: 2

Ahmad
Ahmad

Reputation: 5760

Try to send them all in one array:

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

Upvotes: 0

Related Questions