Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
Are you sure that $_POST["xa"] is an array? and utf8_encode should be inside quotes, otherwise php will throw a notice (or warning). – radalin Apr 6 at 23:03
try: console.log(somearray); just before $.post, and var_dump($_POST['xa'); somewhere in the beginning of your php file. – chester1000 Apr 6 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"}}] – jycr753 Apr 6 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? – icktoofay Apr 6 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 – jycr753 Apr 6 at 23:23
show 2 more comments

3 Answers

up vote 1 down vote accepted

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

share|improve this answer
thanks mate that totally work. still dont get it. i have magic quotes off :S – jycr753 Apr 7 at 0:00
1  
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. – nl-x Apr 7 at 0:02

$.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);
share|improve this answer
i get this on the console after response in javascript: response - NULL – jycr753 Apr 6 at 23:12
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? – jycr753 Apr 6 at 23:26
@jycr753: Yes, try turning magic quotes off. – icktoofay Apr 6 at 23:27
too good to be good, not luck with that :-/ can the server settings mess with this, besides magic_quotes – jycr753 Apr 6 at 23:35
@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. – icktoofay Apr 6 at 23:38
show 1 more comment

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.)

share|improve this answer
Im using json_decode php.net/manual/en/function.json-decode.php – jycr753 Apr 6 at 23:34
1  
Not according to your question above... – nl-x Apr 6 at 23:40
my bad mate. just a miss spell :) but it is json_decode :) thanks for noticing it :) – jycr753 Apr 6 at 23:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.