I want to write JSON data to a text file using jQuery and PHP. I send the data from JavaScript to a PHP file using
function WriteToFile(puzzle)
{
$.post("saveFile.php",{ 'puzzle': puzzle },
function(data){
alert(data);
}, "text"
);
return false;
}
The PHP file is
<?php
$thefile = "new.json"; /* Our filename as defined earlier */
$towrite = $_POST["puzzle"]; /* What we'll write to the file */
echo $towrite;
$openedfile = fopen($thefile, "w");
$encoded = json_encode($towrite);
fwrite($openedfile, $encoded);
fclose($openedfile);
return "<br> <br>".$towrite;
?>
This works but the output in the file new.json
looks like this:
"{\\\"answers\\\":[\\\"across\\\",\\\"down\\\"],\\\"clues\\\":[],\\\"size\\\":[10,10]}"
I don't want those slashes: how did I get them?