title: Return json data with php
body: <p>I want to return json data but my code is not working. I don't get any error message. I have index.php, ajax.php and db.php. Db.php is working. But my ajax code is not working. Where is my mistake?</p>
  
  <p><strong>index.php:</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script type="text/javascript" src="http://code.jquery.com/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="test" style="width:500px;height:400px; margin:20px auto;"&gt;&lt;/div&gt;
  &lt;script&gt;
      $(window).load(function () {
          $.ajax({
              dataType: "json",
              url: 'ajax.php',
              success:function(data){         
                  $("#test").html(data);
              }
          });
      });
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;      
  </code></pre>
  
  <p><strong>Ajax.php:</strong>   </p>
  
  <pre><code>&lt;?php
  require 'db.php';
  $query="select lat,lng from locations order by id";
  $result = pg_query($link, $query);
  if (!$result) {
  echo "An error occurred_xxx.\n";
  }else {
  $arr = pg_fetch_all($result);
  echo json_encode($arr);
  }  ?&gt;
  </code></pre>