title: How do I use jQuery.post to send JSON data to a php script?
body: <p>This is in my javascript code. The variable dataStore is a valid JSON object and the JSON.stringify works fine - I tested with console.log() to be sure.</p>
  
  <p>I'm new to jQuery and have not written a CGI application in awhile so I may be doing something incorrect here.</p>
  
  <pre><code>jQuery.post("taskmanager.php", JSON.stringify(dataStore), function (returnData) { 
      alert(returnData); 
  });
  </code></pre>
  
  <p>The taskmanager.php script resides in the same folder as my other files ( taskmanager.html, taskmanager.css, taskmanager.js ).  This folder resides on my Windows7 folder <strong>E:\JamesLaderoute\MySoftware\WebApp\TaskManager\code</strong></p>
  
  <p>I'm not sure what to put into the taskmanager.php script. I thought I could do something simple like:</p>
  
  <pre><code>&lt;?php
  
      $data = $_GET["data"];
  
      echo "done";
  
  ?&gt;
  </code></pre>
  
  <p>What I don't understand is, how will the php script know that my JSON stringify information was called "data".  </p>
  
  <p>Also, I thought jQuery.post() takes the script name as the first argument, and then the data to be sent to the script followed by a callback function that gets called when the script returns data via <strong>echo</strong> calls.</p>
  
  <p>The alert() does get called but it's displaying the php script rather than the string "done"</p>
  
  <p>I've searched the internet (a little bit) and found examples that use something called AJAX to send data to a script like php. </p>
  
  <p>I learn best with a small example, is there a good example that points out what I'm missing?  How do I get this to work?</p>
  
  <p>Eventually I plan on taking whatever the JSON data is and saving it to a file on the server.  Do I need to run this all with a real server?  Right now I'm not using one but I do have WAMP setup so I could use that if that is what is required.</p>
  
  <p><strong>Thank you to everyone who posted answers to my problem. I'm editing this post to show the actual code I ended up using to get my application working.</strong></p>
  
  <p>JavaScript code:</p>
  
  <pre><code>alert(returnData); });
          $.ajax({
              url : "taskmanager.php" ,
              type : 'POST',
              data : JSON.stringify(dataStore),
              success : function(res) {
                      // Successfully sent data
                    console.log(res);
              },
              error: function(err) {
                  // Unable to send data
                    console.log(err);
              }
          });
  </code></pre>
  
  <p>PHP code:</p>
  
  <pre><code>&lt;?php
  
      $contents = file_get_contents('php://input');
      $data = json_decode( $contents );
  
      file_put_contents( "taskdata.json",  "data=".$contents );
  
      echo "&lt;br&gt;done&lt;/br&gt;";
  
  ?&gt;
  </code></pre>