1

Hey guys i really need help with this. i pass this json object to php..

var x = {};
x.xt = {};
x.xt.id = id;
x.xt.to = foo;

somearray.push(x);

convert object to json:

$.toJSON(x);

json string:

[{"x":{"xt":"9","to":"2"}}]

them i post this:

$.post(
    "temp/sop.php",
    { xa: somearray},
    function(data){
        console.log("response - "+ data);
});

server side:

$xtj = $_POST["xa"];
$encodedArray = array_map(utf8_encode, $xtj);
$asnk = json_decode($encodedArray);

This returns:

string(4) "null"

and this:

$asnk = json_encode($xtj);

returns:

null

the data base it is set to:

UTF8

also when i test if it is an array, comes back true..

any idea how to solve this? thanks

also server side:

$xtj = $_POST["xa"];
$asnk = json_decode($xtj);

this returns:

NULL
7
  • Are you sure that $_POST["xa"] is an array? and utf8_encode should be inside quotes, otherwise php will throw a notice (or warning). Commented Apr 6, 2013 at 23:03
  • try: console.log(somearray); just before $.post, and var_dump($_POST['xa'); somewhere in the beginning of your php file. Commented Apr 6, 2013 at 23:05
  • if i echo the POST i get this: [{\"x\":{\"xt\":\"4\",\"to\":\"2\"}}] and console before $.post is this: [{"x":{"xt":"9","to":"2"}}] Commented Apr 6, 2013 at 23:06
  • What are you trying to do? Is sop.php just supposed to decode the JSON, re-encode it as JSON, and send it back or what? Commented Apr 6, 2013 at 23:14
  • sop.php it is just decoding JSON and passing the array to another method.But the thing it is. it worked fine on one server and local host. but when i moved to their final server it does not work.. even tho it does have the same settings Commented Apr 6, 2013 at 23:23

3 Answers 3

3

$.toJSON(x) does not do the conversion in-place; it returns the JSON, and you're just discarding it. You need this instead:

$.post(
    "temp/sop.php",
    { xa: $.toJSON(somearray) },
    // ...
});

Then, on the PHP side, you won't want array_map as it's not going to be an array until you decode the JSON:

$xtj = $_POST["xa"];
$encodedArray = utf8_encode($xtj);  // I'm not sure you need this, by the way.
$asnk = json_decode($encodedArray);
Sign up to request clarification or add additional context in comments.

4 Comments

i get this on the console after response in javascript: response - NULL
This is the what the POST recieve on the server: [{\"cart\":{\"itemid\":\"4\",\"itemAmount\":\"1\"}}] but after json_encode i get a null result maybe magic_quotes?
too good to be good, not luck with that :-/ can the server settings mess with this, besides magic_quotes
@jycr753: Maybe. I haven't dealt with PHP in a while; I can't think of what would do it off the top of my head.
1

try using if(get_magic_quotes_gpc()) $xtj = stripslashes($xtj); to lose the excessive escaping before trying to decode.

2 Comments

thanks mate that totally work. still dont get it. i have magic quotes off :S
guess not. try using phpinfo() to see which php.ini is actually being used (and to confirm that magic quotes is actually on). Then turn off the magic quotes in the php.ini.
0

What you are doing is you are converting to json string in JS ($.toJSON()). And then in PHP you are again trying to convert to json string (json_encode()). And you are using array_map() on something that is not an array but a string. (Try echo $_POST["xa"]; to see the contents of it.)

1 Comment

my bad mate. just a miss spell :) but it is json_decode :) thanks for noticing it :)

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.