I have an application that takes a JavaScript object, turns it into a string using JSON.stringify(), and passes this string in an ajax POST request. The JavaScript object contains various data, including some user input.
After the data is sent via the POST request, it is decoded using json_decode() to be processed as an array. From this point, the data returned from the application is null.
The user input in the description property contains additional double quotes which causes the data to be invalid, I believe.
How can I escape the double quotes that are entered by the user?
Example Data:
{ name: "Susan", title: "Director", description: ""TEST""}
Note: This is a simple example of the data. The actual data has nested objects.
JavaScript:
let obj = this.objData,
json = JSON.stringify(obj)
$.ajax({
method: 'post'
url: url,
data: {
method: 'submit',
form_data: json
}
});
PHP:
$action=$_POST['method'];
$data=$_POST['form_data'];
$data=str_replace('\"','"',$data);
$data=str_replace("\'","'",$data);
$data=json_decode($data,true); // returns null