I am trying to save some data on the server but finding some encoding problems.
This is how I am sending my objects to PHP (which works fine but I don't think the contentType is actually doing anything):
$.post(
'myfile.php',
{
contentType: "application/x-www-form-urlencoded;charset=utf-8",
data : myData
},
function (data, textStatus, jqXHR){
//some code here
}
);
and then in PHP (myfile.php):
<?php
header('Content-Type: text/html; charset=utf-8');
$file = 'data/theData.txt'; //the file to edit
$current = file_get_contents($file); // Open the file to get existing content
$a2 = json_decode( $current, true );
$data = $_POST['data']; //the data from the webpage
$res = array_merge_recursive( $data, $a2 );
$resJson = json_encode( $res );
// Write the contents back to the file
file_put_contents($file, $resJson);
?>
As you can see I am getting the original contents of the file and decoding the json. I am then merging the result with the data sent from the webpage before re-encoding in json and put the contents back.
This all works as expected. However, at one point in my Jquery the data sent contains a wide variety of symbols like "/ ö é ł Ż ę"
When the file is saved each '/' has an escape character '\' in front of it and likewise the 'é' for example is '\u00e9'
How do I override this? Should I try to convert it properly in the PHP or is their a way in the JQuery to convert back to the correct format after I have $.get('data/theData.txt'?
Any light shed on this issue is greatly appreciated! Please excuse the poor variable names.