How to prevent json-data sent via form to have slashes added? [duplicate]

This question already has an answer here:

Is there a way to prevent JSON-data sent via a form to be escaped with slashes when retrieved with PHP?

I know how to remove slashes, but why is it even escaped? Can I prevent it? If I post json-data via ajax there is no slashes added.

HTML:

<form method="POST">
  <textarea name="data">[{"id":85210094,"quantity":1}]</textarea>
  <input type="submit" value="POST">
</form>

PHP:

<?php
if($_POST['data']) {
  echo '<pre>';
  var_dump($_POST['data']); // string(34) "[{"id":85210094,"quantity":1}]"
  var_dump(json_decode($_POST['data'], true)); // NULL
  var_dump(json_decode(stripslashes($_POST['data']), true)); // array(1) { [0]=> array(2) { ["id"]=> int(85210094) ["quantity"]=> int(1) } }
  echo '</pre>';
}
?>


Source: forms

Leave a Reply