-2

I'm passing a simple JSON array with 4 words to PHP. I want to store that array in a database after I serialize it. Since it's an Ajax call I can only investigate any echoed values by json_encode and alerting them in AJAX success function.

Here's my code:

var jsonString = JSON.stringify(ans);
//if I alert jsonString - it shows the proper array
$.ajax({
    type: "POST",
    url: "script.php",
    data: jsonString, 
    cache: false,

    success: function(data){
        alert(data);
    },
    error: function(){
        alert("error");     
    }
});

That's what I do in PHP with the array:

$answerAr = json_decode($_POST['data']);
$answers = serialize($answerAr);

If I echo the json_encode($answerAr) it alerts NULL in Ajax and $answers turns into 'N;'

Json_last_error returns 0.

kiria67
  • 19
  • 6
  • You might be interested in [this](http://stackoverflow.com/questions/12600956/is-it-good-practice-to-use-serialize-in-php-in-order-to-store-data-into-the-db) thread :) – D4V1D Jun 16 '15 at 09:07
  • 2
    There is no value with the key `jsonString` in the post data. That is what you named in on the client side. There is no way for PHP to know that automagically. Just use `data: ans,` – PeeHaa Jun 16 '15 at 09:10
  • Related: http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php and http://php.net/manual/en/function.var-dump.php – PeeHaa Jun 16 '15 at 09:12
  • @PeeHaa changes made - but it still gives NULL – kiria67 Jun 16 '15 at 09:16
  • Have you also read the two resources Iinked or do you want us to debug your code for you? – PeeHaa Jun 16 '15 at 09:17
  • Have you tried using php://input ? – Obscure Geek Jun 16 '15 at 09:19
  • @PeeHaa I tried it - I cannot see any var_dumps in the console - errors are: Notice: undefined index data - it's for the '$answerAr = json_decode($_POST['data']);' – kiria67 Jun 16 '15 at 09:24
  • Could you print out the var jsonString? I think this is the key – Eason.Luo Jun 16 '15 at 09:57
  • @Eason.Luo I was trying to pass JSON data directly - the solutions to this are already contained in the accepted answer – kiria67 Jun 16 '15 at 10:04

2 Answers2

1

If you're posting data directly to PHP, you'll have to use php://input and parse that; data given in JSON isn't form data, and wont auto-populate the request superglobals ($_GET, $_POST).

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

Most frameworks do this transparently for you and populate a request object with data in it. You should probably check if the request sends JSON data in the body via headers (Content-Type, Accept: application/json, etc)

Alternatively, you can change your AJAX call to give it an array key:

$.ajax({
  data: {data: jsonString},
  // etc
});

Which will then be accessible as $_POST["data"]

Amelia
  • 2,967
  • 2
  • 24
  • 39
0

Change your ajax data attribute to like this and try

data: {json: jsonString}

and in the php script you can access it by

$answerAr = json_decode($_POST['json']);

Harigovind R
  • 816
  • 8
  • 17