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.
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 .ajax 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):
ajax.php (not working):
$(function () {
function fetchData() {
$.ajax('ajax.php');
<?php
try
{
$now = time();
$query = "select * from jet_pressure;"
$result = $db->query($query);
foreach ($result as $row)
{
print "d1.push([".$row['timestamp'].",".$row['unknown']."]);";
}
}
catch (Exception $e)
{
print 'Exception : '.$e->getMessage();
}
?>
$.plot($("#placeholder"), [ d1]);
setTimeout(fetchData, 5000);
}
setTimeout(fetchData, 500);
});
What is the recommended way to do this?