MP_Webby
MP_Webby

Reputation: 916

Passing JSON object to PHP with jQuery AJAX

I have 3 different JSON objects I want to concatenate and send over to a PHP file with jQuery AJAX. I can retrieve the JSON object in the PHP file, but cannot figure out how to loop through the results.

Here's what I have so far:

    //my 3 JSON objects:

    var json_obj_1{
        "test1_key":"test1_val",
        "test2_key":"test2_val",
        "test3_key":"test3_val"
    }

    var json_obj_2{
        "test4_key":"test4_val"
    }

    var json_obj_3{
        "json_arr":[
              {
                  "test1":"test2",
                  "test3":"test4"
              }
         ]
    }

   //concat these to send over to the PHP file:
   var my_json_obj = json_obj_1.concat(json_obj_2);

   //I found if I didn't stringify obj_3 they didn't concatenate for some reason
   my_json_obj = my_json_obj.concat(JSON.stringify(json_obj_3));


   //my_json_obj  now looks like this:

   {
        "test1_key":"test1_val",
        "test2_key":"test2_val",
        "test3_key":"test3_val"
   }
   {
        test4_key: test4_val
   }
   {
        "json_obj_3":[
            {"test1":"test2","test3":"test4"}
        ]
   }

Based on this question: Sending JSON to PHP using ajax, I don't stringify the final JSON object.

Here's the AJAX call to the PHP file:

$.ajax({
        url: 'my_php_file.php',
        data: {my_json_data: my_json_obj},
        type: 'POST',
        async: false,
        dataType: 'json',
        cache:false,
        success:function(data, textStatus, jqXHR){
            console.log('AJAX SUCCESS');
        }, 
        complete : function(data, textStatus, jqXHR){
            console.log('AJAX COMPLETE');
        }
});

This is what it looks like when retrieved in my PHP file:

echo $_POST['my_json_data'];
//outputs:
{
    "test1_key":"test1_val",
    "test2_key":"test2_val",
    "test3_key":"test3_val"
}
{
     test4_key: test4_val
}
{
    "json_obj_3":[
        {"test1":"test2","test3":"test4"}
    ]
}

Here's where I run in to problems. I want to be able to loop through this in a foreach. From what I have read (php: loop through json array) I have to decode the JSON object. However when I do this and loop through the results I get: " PHP Warning: Invalid argument supplied for foreach() ".

Even if I stringify the JSON object before sending it over to the PHP file I get the same problem. I have been banging my head against a brick wall for hours with this. Any help will be massively appreciated.

Thanks in advance.

Solved:

I was going wrong when concatenating my JSON objects following this question: Merge two json/javascript arrays in to one array. Instead I just put them together like so:

my_json_obj = '['+json_obj_1+','+json_obj_2+', '+JSON.stringify(json_obj_3)+']';

I then use json_decode in my PHP file which works a treat. http://json.parser.online.fr/ was invaluable in debugging my dodgy JSON. A great tool for JSON beginners.

Upvotes: 2

Views: 10985

Answers (2)

Wilmer
Wilmer

Reputation: 2511

You should be stringifying the whole object, try the following:

var my_json_obj = json_obj_1;

$.each(json_obj_2, function(key,value){
    my_json_obj[key]=value;
});
$.each(json_obj_3, function(key,value){
    my_json_obj[key]=value;
});

Ajax request:

data: {my_json_data: JSON.stringify(my_json_obj)},    

PHP:

print_r(json_decode($_POST['my_json_data']));

Upvotes: 3

Joe Pietroni
Joe Pietroni

Reputation: 826

As you say you need to decode the JSON, but your JSON is not well formed so I think the decode function is failing which causes the foreach error. There needs to be a , between objects, there's missing quotes around test4_val and it needs to be inside a singular object/array

You can check your JSON is wellformed at:-

http://json.parser.online.fr/

I think you want it to look more like:-

[{
"test1_key":"test1_val",
"test2_key":"test2_val",
"test3_key":"test3_val"
},
{
 "test4_key": "test4_val"
},
{
"json_obj_3":[
    {"test1":"test2","test3":"test4"}
]
}]

Upvotes: 1

Related Questions