0

i'm working on this now for about 2-3 hours and i cant find where i'm doing it wrong. this is my jQuery that is going to build an object:

var data = {cat:[],count:[],size:[],type:[],print:[]};
$("#Cat-list option").each(function()
    {data.cat.push($(this).val());});
$("#Count-list option").each(function()
    {data.count.push($(this).val());});
$("#Size-list option").each(function()
    {data.size.push($(this).val());});
$("#Print-list option").each(function()
    {data.print.push($(this).val());});
$("#Type-list option").each(function()
    {data.type.push($(this).val());});

after this i will have an object named data. when i convert the obj to JSON by var jsonString=JSON.stringify(data); it gives me something like this:

{
    "cat":["Cart Visit","Bag","Envelope","Tracket","Brosur"],
    "count":["1000","2000","4000","5000","?????"],
    "size":["S","M","L","X"],
    "type":["?? ??","??? ? ??","????"],
    "print":["????","???????","????"]
}

then i use jQuery Ajax to send the jsonstring to my php file like this:

$.ajax({
    type: "POST",
    url: 'update_db.php',
    data: jsonString,
    contentType: "application/json; charset=utf-8",
    success:
    function(result) {      
        $( "#alert").html( result );
    }               
});

and finally i'm trying to recieve the data with php script. i dont know how to fetch the data for this i tried it with 'jsonstring' and 'data':

$json = json_decode( $_POST['jsonstring']);
$data = json_decode( $_POST['data']);
var_dump($json);
var_dump($data);

but both are "NULL". What am I doing wrong?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Babakslt
  • 199
  • 1
  • 10
  • I think when you're sending the data to php from ajax, you have to do this: `data: {'jsonstring': jsonString}`. Then when getting the post data, you can do `json_decode($_POST['jsonstring'])`. Your jsonString will be in the post variable `jsonstring`. – sulavvr Nov 06 '13 at 17:17
  • possible duplicate of [How to get body of a POST in php?](http://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php) – Quentin Nov 06 '13 at 17:20
  • @flyingDuck - serialising data as `application/json` and then *again* as `application/x-www-form-urlencoded` is not ideal. The asker has deliberately specified `contentType: 'application/json'`, so I assume that's what they actually want. – cloudfeet Nov 06 '13 at 17:26
  • @cloudfeet oh! I didn't think about that. Thanks for the comment. So, how will you be able to get the string when you don't have a post variable? – sulavvr Nov 06 '13 at 17:36
  • @flyingDuck - You can use `php://input`, which is a pseudo-file that represents the raw data that was POSTed. Here, it will be exactly equal to the value of `jsonString`. – cloudfeet Nov 06 '13 at 17:38
  • @flyingDuck - for an illustration, see the comments on my answer. Having tried to apply *both* of our answers, the asker is ending up with `jsonstring=%7B%22cat%22%3A%5B%22Cart+Visit%22%2CF%3F%22%5D%7D...` - this ugly mess is what is actually transmitted (under the hood) when you do this kind of double-encoding. – cloudfeet Nov 06 '13 at 17:48
  • @cloudfeet Oh! I'll avoid that in the future. Thanks for the awesome piece of information. :) – sulavvr Nov 06 '13 at 19:55

1 Answers1

1

When you POST JSON (application/json) to PHP, it doesn't automaticaly get parsed (unlike the traditional encoding, application/x-www-form-urlencoded). You need to read it in manually:

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

You are getting NULL because that's what json_decode() returns for invalid input ($_POST is empty, so there's nothing to decode).

P.S. - php://input is a special filename for the raw data that has been submitted by POST/PUT/whatever, regardless of the encoding used. See the php.net manual for full documentation.

cloudfeet
  • 12,156
  • 1
  • 56
  • 57
  • One can also send a JSON-formatted string as ordinary POST parameter. – Marcel Korpel Nov 06 '13 at 17:20
  • That's true, but that's wrapping one serialisation format inside another - it's both inelegant and inefficient (particularly given that a JSON document will be full of punctuation marks that will need to be percent-encoded). – cloudfeet Nov 06 '13 at 17:21
  • tnx. now i have the data in $jsonText but json_decode($jsonText) still not working. when i var_dump the $jsonText there's somthing llike this: string(449) "jsonstring=%7B%22cat%22%3A%5B%22Cart+Visit%22%2CF%3F%22%5D%7D" but with much more data. – Babakslt Nov 06 '13 at 17:36
  • @Babakslt: That is because you modified your code, particularly `data: jsonString` in the JavaScript. Reset it to the way it is in your question, then try again. :) – cloudfeet Nov 06 '13 at 17:39
  • That `string(499)` is due to `var_dump()` - it is not part of the actual data. You have to reset the code to the original state (undoing the modification that flyingDuck proposed), and then it should work. – cloudfeet Nov 06 '13 at 17:44
  • yes yes for sure :))) as i said i'm too confused right now. AND THANKS A LOT. it's working :D THANK YOU – Babakslt Nov 06 '13 at 17:52