1

I'm trying to pass a some information to a php page with jquery ajax. I really can't understand why the php page keeps echoing back that the strinified JSON i'm sending is null

//php
if ($type == "new" || $type == "update"){
    $new_address = json_decode($_REQUEST['address'], true);
    echo json_encode($new_address); //Null
}

//js
var string_encoded_address = JSON.stringify(address_obj.address);
string_encoded_address = encodeURIComponent(string_encoded_address);
console.log(string_encoded_address);
$.ajax({
    type: "post",
    url: "order_queries_templates/queries/address.php",
    data: "type=new&user_id=" + user_id + "&address=" + string_encoded_address,
    dataType: "json",
    success: function (returnedData) {
        console.log(returnedData);
    }
});

this gives me a string for my data property:

type=new&user_id=8244&address=%7B%22companyName%22%3A%22test%20company%22%2C%22address1%22%3A%222420%20sample%20Road%22%2C%22city%22%3A%22SIOUX%20CITY%22%2C%22state%22%3A%22IA%22%2C%22zip%22%3A%2251106%22%2C%22cityStateZip%22%3A%22SIOUX%20CITY%2C%20IA%2051106%22%7D 

What could be wrong with it? Thanks!

4
  • I think you have to urldecode($_REQUEST['address']) before you can use it. Commented Feb 13, 2013 at 22:59
  • Try stringifying the ENTIRE query you set up and pass as data. Commented Feb 13, 2013 at 23:02
  • @FabianBlechschmidt okay tried this...didn't really have any effect. thanks though Commented Feb 13, 2013 at 23:10
  • what is var_dump($_REQUEST['address'])? Commented Feb 13, 2013 at 23:11

1 Answer 1

1

The reason why your code doesn't work is because you have magic_quotes_gpc enabled. It adds escaping to the double quotes, as can be seen using this cli script:

$s = 'address=%7B%22companyName%22%3A%22test%20company%22%7D';
// parse query string into array
parse_str($s, $a);
// print address portion
echo $a['address'], "\n";

php -dmagic_quotes_gpc=On test.php

Output:

{\"companyName\":\"test company\"}

The additional escaping breaks json_decode() so it returns null.

Turning magic_quotes_gpc off will fix this particular issue, either by using .htaccess or editing the php.ini.

However, it's far easier to just let jQuery take care of the serialization for you:

$.ajax({
    ...,
    data: {
      type:'new', 
      user_id: user_id, 
      adress: address_obj.address
    },
    ...
});

In this case you won't have to json_decode() on the server anymore, just reference $_POST['address'] directly.

Sign up to request clarification or add additional context in comments.

5 Comments

@thomas No problem; do take note of the second part of my answer that discusses an easier way to handle data sending.
it will just recognize address_obj.address as a json object and do that stringification for me and php will understand it as an associative array the same as it would json_decode($var, **TRUE**)?
@thomas Wat? I have no idea what you're talking about, sorry :)
no, i'm sorry. not very clear. When I pass the second parameter true to json_decode(), it turns the JSON string into an associative array. I just wondered how I will get an associative array from the JSON when I have removed the json_decode() from the server. Like you said in the last two lines of your answer. EDIT:removed a question I now see you've already answered in the last line. Thanks for all the help!
@thomas using the last bit of my answer is similar to what you would achieve with the true as second parameter to json_decode() :)

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.