Read associative array from json in $_POST

68,398

Solution 1

You can avoid to use JSON.stringify and json_decode:

jQuery.post("save.php", dataToSend, function(data){ alert(data); });

And:

<?php
echo $_POST['page'];
?>

Update:

... but if your really want to use them, then:

jQuery.post("save.php",  {json: JSON.stringify(dataToSend)}, function(data){ alert(data); });

And:

<?php
$value = json_decode($_POST['json']);
echo $value->page;
?>

Solution 2

$_POST will not be populated if the request body is not in the standard urlencoded form.

Instead, read from the read-only php://input stream like this to get the raw request body:

$value = json_decode(file_get_contents('php://input'));

Solution 3

Pass the second argument as true if you want the associative array otherwise it will keep returning object.

$value = json_decode(stripslashes($_POST),true);

Solution 4

Try:

echo $value->page;

since json_decode's default behaviour is to return an object of type stdClass.

Alternatively, set the second optional $assoc argument to true:

$value = json_decode(stripslashes($_POST), true);
echo $value['page'];

Solution 5

It looks like jQuery might encode a javascript object in urlencoded form then would be populated into $_POST. At least from their examples. I'd try passing in your object into post() without stringifying it.

Share:
68,398

Related videos on Youtube

Daniel
Author by

Daniel

Updated on August 06, 2020

Comments

  • Daniel
    Daniel almost 4 years

    I am using jQuery to post a json object to my php application.

    jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });
    

    The json string as pulled from firebug looks like this

    { "data" : [ { "contents" : "This is some content",
            "selector" : "DIV.subhead"
          },
          { "contents" : "some other content",
            "selector" : "LI:nth-child(1) A"
          }
        ],
      "page" : "about_us.php"
    }
    

    In php I am trying to turn this into an associative array.

    My php code so far is

    <?php
    $value = json_decode(stripcslashes($_POST));
    echo $value['page'];
    ?>
    

    The response to the ajax call should be "about_us.php" but it comes back blank.

  • Daniel
    Daniel about 13 years
    Thank you, Your first method was by far the simplest solution and it worked! I tried the second yesterday and could not get it to work.
  • gumuruh
    gumuruh about 12 years
    is true that it is enough if my POST data wasnot data=value content then I shall use file_get_contents() instead of $_POST ? @Evert?
  • Namrata Das
    Namrata Das about 12 years
    It depends on the content-type that's sent.
  • gumuruh
    gumuruh about 12 years
    depend on content-type? But in my code i was using content-type of "Content-type", "application/json" and yet... in the Server itself I need to use file_get_contents() instead of $_POST. sigh.
  • Namrata Das
    Namrata Das about 12 years
    Yes, because application/json is not one of the content-types that will populate $_POST. Only application/form-data and application/x-www-form-urlencoded will cause this to be parsed. file_get_contents is actually the best way to do this, the solution the OP ended up using is not as elegant.
  • Michal Stefanow
    Michal Stefanow about 11 years
    Warning: stripslashes() expects parameter 1 to be string, array given
  • Khurshid Alam
    Khurshid Alam over 9 years
    @Evert $HTTP_RAW_POST_DATA has been depreciated in php 5.6. Now whenever I use file_get_contents('php://input')' to process json_encoded data (on my php-scrpt hosted on heroku), it complains about it & want me to set 'always_populate_raw_post_data' to '-1'. But If I do that It can not populate the data in $value . What to do now?
  • Mircea Sandu
    Mircea Sandu over 8 years
    the stripslashes is great if you are sending multiple post variables and one of them is a json string
  • Mark Amery
    Mark Amery over 8 years
    -1; WTF? This will never work under any circumstances ever. $_POST is always an array, and stripslashes will never accept an array as an argument. The code you've given is guaranteed to throw a warning and set $value to null, regardless of the POST body.