-2

I'm new to php,JSON, and js.
My problem is my php has somehow receive unquote json string from js.
I already print the json string after passing it through the function JSON.stringify. and i get a normal json string with quote

This is my js script:

function post()
{
  var test1 = {
    name:"marzzuq",
    age:16
   };
myjson = JSON.stringify(test1);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","post.php",true);
xmlhttp.setRequestHeader("Content-type","application/json");
xmlhttp.send(myjson);
console.log("\nsend ok: " + myjson);
}

and i get this from console.log send ok: {"name":"marzzuq","age":16}

and this is my php script:

<?php
$rawdata = file_get_contents('php://input');

`echo $rawdata >> /tmp/test.txt`;

$JSON = json_decode($rawdata,true);
` echo test2 > /tmp/test.txt` ;
switch (json_last_error()) {
    case JSON_ERROR_NONE:
        `echo ' - No errors' >> /tmp/test.txt`;
    break;
    case JSON_ERROR_DEPTH:
        `echo ' - Maximum stack depth exceeded' >> /tmp/test.txt`;
    break;
    case JSON_ERROR_STATE_MISMATCH:
        `echo ' - Underflow or the modes mismatch' >> /tmp/test.txt`;
    break;
    case JSON_ERROR_CTRL_CHAR:
        `echo ' - Unexpected control character found' >> /tmp/test.txt`;
    break;
    case JSON_ERROR_SYNTAX:
        `echo ' - Syntax error, malformed JSON' >> /tmp/test.txt`;
    break;
    case JSON_ERROR_UTF8:
        `echo ' - Malformed UTF-8 characters, possibly incorrectly encoded' >> /tmp/test.txt`;
    break;
    default:
        `echo ' - Unknown error' >> /tmp/test.txt`;
    break;
}
?>

at the end of the function, when i access /tmp/test.txt, i get this:

{name:marzzuq,age:16}

Why i get an unquote string? I was supposed to get quoted string correct? something like this:
{"name":"marzzuq","age":16}

maximran
  • 425
  • 4
  • 11
  • 1
    Try `echo {"name":"marzzuq","age":16}` in your bash terminal. See the problem? I suggest you use a PHP file method like `file_put_contents()` instead of [execution operators](http://php.net/manual/en/language.operators.execution.php) – Phil Jan 22 '18 at 04:45
  • Voting to close as a typo – Phil Jan 22 '18 at 04:48
  • @Phil, It seems different terminals have different behaviors. When I use windows command line, it will write the right json to txt file. When I use the mintty terminal (windows git bash terminal provided by GIT), it will write `name:marzzuq age:16` to txt file. from another Phil. :) – Phil Jan 22 '18 at 05:00
  • @Phil `/tmp` indicates a *nix system. – Phil Jan 22 '18 at 05:07
  • @Phil Thanks. I am just wondering which terminal XAMPP will use since I have more than one. – Phil Jan 22 '18 at 05:09
  • 1
    Thanks for the clarification. I did not notice that echo {"name":"marzzuq","age":16} will get me string without quotes. Yes, my system is freebsd. and the above code is just a simple code to see if it was possible to send and receive JSON data on my server. – maximran Jan 22 '18 at 05:16
  • For xampp in Windows environment, it seems it will use the windows command line tool although I have several terminal tools installed. It (windows command line tool) will not have this problem. It's interesting to see the differences. – Phil Jan 22 '18 at 05:19
  • may be you can try to write your data into JSON file instead of txt file. like tmp/test.json – Gautam Patadiya Jan 22 '18 at 05:23
  • @GautamPatadiya that's going to make absolutely zero difference – Phil Jan 22 '18 at 05:23
  • well in my case when I store json data I uses json_ecode(data,JSON_HEX_APOS) and when I get it back with decode it's auto adjust signle and double quote you can try may be will works fine in your case – Gautam Patadiya Jan 22 '18 at 05:27
  • @GautamPatadiya I have tried your solution, but it does not work for me. The best method is to used `file_put_content`.Thanks for the suggestion :) – maximran Jan 24 '18 at 01:16

1 Answers1

-1

As mention by @phil, it seem that the reason i got unquote string is because i try to save into file using the execution operator+ using echo. I should used file_put_contents to save the input receive, to preserve the format.

maximran
  • 425
  • 4
  • 11