title: String JSON-encoded with ExtJS can't be decoded with json_decode in PHP
body: <p>I'd like to store data from an input field to a database. However, when the user decides to enter some backslashes or quotes, i'm screwed.</p>
  
  <p>I'm using the following code on the client side. The value of mystring is in fact read from an input field. </p>
  
  <pre><code>mydata = Ext.encode({
      mystring : "foo '\' bar ' \0x00\""
  });
  
  Ext.Ajax.request({
    url: '/test.php',
    params: { data: mydata }
  });
  </code></pre>
  
  <p>On the server, <code>print_r($_POST)</code> gives me </p>
  
  <pre><code>array(
    ['data'] =&gt; '{"mystring":"foo '' bar ' \u0000x00\""}'
  )
  </code></pre>
  
  <p>So, I lost the backslash after foo, and the <code>\0x</code> became <code>\u00</code>, and </p>
  
  <pre><code>var_dump(json_decode($_POST['data'])
  </code></pre>
  
  <p>fails and returns</p>
  
  <pre><code>NULL
  </code></pre>
  
  <p>In an other case, when the user uses quotes, I get </p>
  
  <pre><code>mydata = Ext.encode({
      {"mystring":"foo '\"' bar '"}
  });
  </code></pre>
  
  <p>and I end up on the PHP side with <code>$_POST</code></p>
  
  <pre><code>array(
    ['data'] =&gt; '{"mystring":"foo '"' bar '"}'
  )
  </code></pre>
  
  <p>which too is no valid input for json_decode().</p>
  
  <p>I want the user input literally. I'd like to let my database wrapper take care of the escaping stuff before the INSERT command, but how can I send arbitrary strings, potentially containing any amount of slashes, backslashes, double and single quotes safely to my PHP script and json_decode it successfully?</p>
  
  <p>I already thought about base64 encoding, but that is not very much an option. </p>
  
  <p>See <a href="http://dev.sencha.com/deploy/dev/docs/?class=Ext.util.JSON" rel="nofollow">http://dev.sencha.com/deploy/dev/docs/?class=Ext.util.JSON</a> for documentation.</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>The data is read from the store of an Ext.grid.EditorGridPanel myGrid. The whole script has about 2.500 LoC, so I only post an excerpt here. I also changed some variable names to make the whole stuff more readable.</p>
  
  <pre><code>var modRec = myGrid.getStore().getModifiedRecords();
  var data = new Array();
  var len = modRec.length - 1;
  for (f = len; f &gt; -1; --f) {
      var a = {};
      var changes = modRec[f].getChanges();
      for (var name in changes) {
          if ('function' == typeof changes[name]) {
              continue;
          }
          a[name] = changes[name];
      }
      data.push(a);
  }
  
  Ext.Ajax.request({
      url: '/test.php',
      params: { Ext.encode(data) }
  });
  </code></pre>
  
  <p>The result is the same as above. When a user enters a double quote into the editor field of the grid cell editor, which is an Ext.form.TextField, the string is not encoded and decoded correctly.</p>