title: How to send JSON to PHP through AJAX?
body: <p>I've tried over and over and it never works. I don't know how to receive the JSON on the server side. </p>
  
  <p><strong>JS</strong></p>
  
  <pre><code>function newReport_compilation(){
   ...
   return JSON.stringify(formData);
  }
  var formData = newReport_compilation(); //takes all values from my form
  ...
  $("#newReport_submit").on('click',function(){
  ajaxRequestTwo("databaseButler.php?reqType="+2, "text", formData, function(returnedData){
  console.log(returnedData); //To test
  ...
  }}
  
  function ajaxRequestTwo(reqScript, returnDataType, reqData, callback){
      $.ajax({
          type: "POST",
          dataType: returnDataType,
          url: reqScript,
          data: {fData:reqData},
          contentType: 'application/json; charset=UTF-8',
          success: function(data) {
              console.log("AJAX request success.");
              callback(data);
          },
          fail: function(){
              console.log("AJAX request failed.");
          },
          error: function(){
              console.log("Error!");
          }
      });
  }
  </code></pre>
  
  <p><strong>PHP (DatabaseButler.php)</strong></p>
  
  <pre><code>function reportInsert(){
      def();
      //Establish a new connection
      $repInsCon = new Db();
      //Run the insertReport function from the DB class (Send it the report ID &amp; the JSON data of the submitted form)
      $result = $repInsCon-&gt;insertReport($_POST['fData']);
      echo $result;
  }
  if(isset($_GET['reqType'])){
      ...
      }else if($_GET['reqType']=="2"){
          reportInsert();
      }
  }
  </code></pre>
  
  <p><strong>Deeper PHP (inside Db() class)</strong></p>
  
  <pre><code>public function insertReport($jsonForm){
  $newRepData = json_decode($jsonForm,true);
  echo $jsonForm;
  ... //I have the SQL INSERT query part commented out here but I can't get here regardless
      }
  </code></pre>
  
  <p>It gives me an error saying <strong>Notice: Undefined index: fData in C:\wamp\www\HCSProjects\Pigeon\Main\databaseButler.php</strong></p>
  
  <p>I don't understand how you receive the JSON data on the PHP side? What is the JSON called once it reaches PHP? How do I receive it properly on the server side?
  :/</p>
  
  <p>I've been looking into this for hours on end. There's no proper answer anywhere.</p>
  
  <p><strong>UPDATE</strong><br>
  The AJAX request works fine because I get a 4 and a 200 on the response. 
  The following line in the butler.php file is the problem: (I removed the reqType by the way.... i just made the url databaseButler.php with no additional parameters).</p>
  
  <pre><code>if(isset($_POST['req'])){
  </code></pre>
  
  <p>'req' in the above line is one my JSON values. I console.logged the JSON before I sent it to PHP. (My JSON is reqData. I console.logged formData which is the same thing)</p>
  
  <p>{"id":"1615","na":"Y","ph":"905-525-9140","em":"h@mcmaster.ca","dep":"Residence Admissions","req":"PC Issues","cus":"","summ":"diwjdiwjdi","det":"","pri":"Low","dat":"07/08/2015","ti‌​m":"anytime"}</p>
  
  <p>This is my newReport_compilation function by the way:</p>
  
  <pre><code>function newReport_compilation(){
      var formData = {id:hash(1),na:$("#newReport_name").val(),ph:$("#newReport_phone").val(),em:$("#newReport_email").val(),dep:$("#newReport_department").val(),req:$("#newReport_requestCategory").val(),cus:$("#newReport_otherRequest").val(),summ:$("#newReport_summary").val(),det:$("#newReport_details").val(),pri:$("input[type='radio'][name='priority']:checked").val(),dat:$("#newReport_date").val(),tim:$("#newReport_time").val()};
      return JSON.stringify(formData);
  }
  </code></pre>
  
  <p>How do I get my JSON values on the PHP side? There's no help anywhere guys :/</p>
  
  <p><strong>UPDATE: FOUND A SOLUTION</strong></p>
  
  <p>For those interested:<br>
  The problem was the JSON.stringify(formData) in newReport compilation. Apparently you don't have to stringify formData if it was stored in JSON like form. It was like I tried to convert an already JSON string to JSON, which broke it.</p>
  
  <p>Also, when you send data in both the URL and in JSON form, access everything using $_REQUEST and not $_POST or $_GET. However any JSON data you send over goes by the format you send the data. In my case I used POST. So to get ID or name or the server side (DatabaseButler PHP) you would use $_POST['id'] or $_POST['summ'].</p>