1

I have an array or a javascript object that I create like this: arr[arr.length]=obj where the obj is a classic JSON string like {"id":1}.

So arr seems to be an array of JavaScript Objects.

I can access to it like this: arr[1], arr[2]. It could be even like alert(arr[1].id);

If i do: alert(JSON.stringify(arr)); I get the following:

[{"id":"2305","col":"1"},{"id":"2308","col":"1"},{"id":"2307","col":"1"},{"id":"2306","col":"1"}]

Whereas alert(arr); Gives me something like:

[object Object],[object Object],[object Object],[object Object],[object Object]

Now I need to pass it to a PHP script using jQuery's AJAX method. But it seems that it can get only combined strings like:

{"id":"2305","col":"1"} or {"id":"2305","col":"1","id":"2305","col":"1"}

But JSON.stringify parses the arr object successfully and my previous example of string I have seems to be a valid JSON string. How can I pass to PHP, should I really change the whole format of structure to be like the last sample?

UPD: i forgot to mention that PHP's POST array is null if send '{},{},{}' string to it rather then '{}' string.

UPD: I rewrote the code that was generated the string. Now i have a string like this:

{"2305":"1","2306":"1"}

It works if i pass it to PHP directly, like this:

   $.post({url: '../getItems2Cart.php', data:{"2305":"1","2306":"1"} ,
       success: function(response){alert(response);}
   });

If i send it like this, php return empty POST array:

$.post({url: '../getItems2Cart.php', data: JSON.stringify(str),.
       success: function(response){alert(response);}
});

To get clear, alert returns a proper JSON strong now:

alert('json str to php '+JSON.stringify(str));
//json str to php {"2305":"1","2306":"1"}

Ahh.. yes, and str is an javascript object, not string.

1 Answer 1

7

Can send the JSON and use json_decode() to turn it into a php array.

$.post('server/path', { jsonData: JSON.stringify(arr)}, function(response){
   /* do something with response if needed*/
});

in php:

$arr=json_decode( $_POST['jsonData'] );
/* return first ID as test*/
echo $arr[0]['id'];
/* or dump whole array as response to ajax:*/
print_r($arr);
Sign up to request clarification or add additional context in comments.

11 Comments

Sorry, i forget to mention that PHP's POST array is null if send {},{},{} string to it rather then {} string.
not clear what you are sending. If it matches the code after I get the following: in question should work fine. I don;t understand what {},{},{} vs {} means. First should be an array [{},{},{}]
you aren't following my example and setting a key for the JSON string. POST needs key/value pairs... if you just send string you are only sending a value so $_POST will be empty
Probably i didn't get your example. If i sent the string directly - it works. If i stringify the object, it doesnt. There is probably a tiny detail, i've missed.
what do you mean didn't get it?? ... it's the code in my answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.