title: Returning a JSON object from PHP in AJAX call?
body: <p>Here's my PHP code called during jQuery AJAX call:</p>
  
  <pre><code>&lt;?php
      include '../code_files/conn.php';
      $conn = new Connection();
      $query = 'SELECT Address_1, Address_2, City, State, OfficePhone1, OfficePhone2, Fax1, Fax2, Email_1, Email_2 
                FROM clients WHERE ID = ?';
      $conn-&gt;mysqli-&gt;stmt_init();
      $stmt = $conn-&gt;mysqli-&gt;prepare($query);
      $stmt-&gt;bind_param('s', $_POST['ID']);
      $stmt-&gt;execute();
      $result = $stmt-&gt;get_result();
      $row = $result-&gt;fetch_assoc();
      echo json_encode($row);
  ?&gt;
  </code></pre>
  
  <p>And the client-side code is:</p>
  
  <pre><code>$.post(url, {ID:$('#ddlClients').val()},
          function(Result){
              // Result
          }
        );
  </code></pre>
  
  <p>The AJAX call is successfully completed. I get the value of <strong>Result</strong> as </p>
  
  <p><code>"{"Address_1":"Divisional Office 1","Address_2":"The XYZ Road",.....and so on</code></p>
  
  <p>What I want is to be able to use the values returned like Result.Address_1, Result.Address_2 and so on. But I can't do it using the above code. I tried using <code>$row = $result-&gt;fetch_object()</code> and <code>$row = $result-&gt;fetch_array()</code>, but no use.</p>
  
  <p>And I know that this can be done by this code on the server side:</p>
  
  <pre><code>$row = $result-&gt;fetch_assoc();
  $retVal = array("Address_1"=&gt;$row['Address_1'], "Address_2"=&gt;$row['Address_2'].......);
  echo json_encode($retVal);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>$row = $result-&gt;fetch_object();
  $retVal = array("Address_1"=&gt;$row-&gt;Address_1, "Address_2"=&gt;$row-&gt;Address_2.......);
  echo json_encode($retVal);
  </code></pre>
  
  <p>Is there a way to send the <strong><code>$row</code></strong> directly to the client side JavaScript and ready to be used as JSON object, without manually creating an array first? </p>