title: How to retrieve values from a JSON Object in PHP
body: <p>I've tried this many times now, using difference methods and none of them have worked for me, so I'm asking this question.</p>
  
  <p>I have a small form that takes in 4 pieces of information, a persons title, first name, middle name and last name. When I hit a button, a JSON Object is formed, and sent as post data through a jQuery.ajax method.</p>
  
  <p>JSON Sent to PHP file:</p>
  
  <pre><code>{
      "title": "Mr", 
      "firstName":"Banana",
      "middleName":"Slippy",
      "lastName":"McDougle"
  }
  </code></pre>
  
  <p>Ajax call on button press:</p>
  
  <pre><code>    function insertPerson(obj, callback){
          $.ajax({
              type: "POST",
              data: "json=" + JSON.stringify(obj),
              url: "insertData.php",
              success: function(obj){
                  if (callback){ callback(obj) };
              },
              error: function(error){
                  console.log("Error:");
                  console.log(error);
              }
          });
      }
  </code></pre>
  
  <p>I pass in a Javascript Object that is then stringyfied and posted as a parameter name 'json'.</p>
  
  <p>In my php file I assign the posted string to a variable, name $json, I then decode using json_decode(), do some funky business, and send a response back to the browser.</p>
  
  <p>insertData.php:</p>
  
  <pre><code>require ('connect.php');
  header('Content-type: application/json');
  
  $json_string = $_POST['json'];
  $json = json_decode($json_string);
  ...do server related inserts/selects etc...
  echo json_encode($json-&gt;title);
  </code></pre>
  
  <p>At the moment I just want to return the title property of the json object that was sent in the post request, but anything I return comes back as null. If I echo back the string, without decoding it, then I get the following:</p>
  
  <pre><code>{\"title\":\"Mr\",\"firstName\":\"Banana\",\"middleName\":\"Slippy\",\"lastName\":\"McDougle\"}
  </code></pre>
  
  <p>If I try to extract values using:
     $title = $json->title;
  and put that into a MYSQL statement, it's inserted as null or blank, nothing gets input.</p>
  
  <p>Am I doing something wrong? Or have I somehow got an outdated version of PHP to handle JSON? And help is greatly appreciated.</p>