title: Getting SQL data back into JavaScript after jQuery ajax call
body: <p>I'm pretty new to ajax (via jQuery) and JavaScript. What I would like is to execute a php script periodically (and asynchronously) that fetches some SQL data. However I will present this data in a JavaScript graph, so I need it to get back into my JavaScript. </p>
  
  <p>I tried an embedded php script inside the JavaScript that pushes the SQL data into an array, and then simply fetches the page itself with <code>.ajax</code> call, but this did not work (even though I could see in the page source that the JavaScript was changed, the graph did not respond to the changes):</p>
  
  <p>ajax.php (not working):</p>
  
  <pre><code>$(function () {        
     function fetchData() {            
     $.ajax('ajax.php');
     &lt;?php        
     try
     {
        $now = time();
        $query = "select * from jet_pressure;"        
        $result = $db-&gt;query($query);
        foreach ($result as $row)
        {                                      
           print "d1.push([".$row['timestamp'].",".$row['unknown']."]);";            
        }                                
     }
     catch (Exception $e)
     {
        print 'Exception : '.$e-&gt;getMessage();
     }
     ?&gt;           
     $.plot($("#placeholder"), [ d1]);
     setTimeout(fetchData, 5000);
     }    
      setTimeout(fetchData, 500);    
     });       
  </code></pre>
  
  <p>What is the recommended way to do this?</p>