title: Save Data to File via Ajax POST and PHP
body: <p>I just want to save some JSON (generated with Javascript) in a file on the server. 
  But I even don't get it to work with a simple string:</p>
  
  <p>HTML File:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
  &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"&gt;&lt;/script&gt;
  &lt;style&gt;
  #test{
  padding:20px 50px;
  background:#ccc;
  color:#000;
  }
  &lt;/style&gt;
  &lt;script&gt;
  $(function(){
  $('#test').click(function(){
      $.ajax({
          url: "page.php",
          data: {"foo": "bar"},
          processData: false,
          contentType: 'application/json'
      });
  
  });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div id="test"&gt;
  KLICK
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And the php file is something like this:</p>
  
  <pre><code>&lt;?php
  $myFile = "testFile.txt";
  $fh = fopen($myFile, 'w');
  fwrite($fh,$_POST['data']);
  fwrite($fh,$_POST['foo']);
  fwrite($fh,$_POST["foo"]);
  fwrite($fh,$_POST[foo]);
  fclose($fh);
  </code></pre>
  
  <p>Nothing worked. I also tried </p>
  
  <pre><code>$.ajax({
      var datatosend="foo bar";
      url: "page.php",
      data: datatosend,
      processData: false
      });
  </code></pre>
  
  <p>I don't know what could be wrong. The txt file is there after clicking the div in the html file. But there is no content in the file. If I just write $_POST to the text file, the file contains the Text "Array", meaning that $_POST has some content. </p>