Charles Tester Charles Tester - 4 months ago 6
jQuery Question

AJAX get() data

I have a block of jQuery which uses the

method in a
. I don't understand how to get data from the second URL the the jQuery code.


<script type="text/javascript" src=""></script>
<script type="text/javascript">
setInterval(function() {
$.getJSON("check_time.php", function(update) {
if (update) {
}, 600000);

PHP - check_time.php

header('Content-type: application/json');
$stmt = $conn->prepare("$sqlst = $conn->prepare("SELECT COUNT(*) AS count
FROM ads
WHERE lastupdate > NOW() - INTERVAL 10 MINUTE");
$row = $sqlst->fetch();");
$stmt ->execute();
$row = $stmt ->fetch();
$update = $row['count'] > 0;
$updtstatus = json_encode($update);
echo "$updtstatus";


I am not getting the variable from the across from check_time.php to the update variable (function(update).


Small alter in php page

$updtstatus =  json_encode(array('count'=>$update));
echo $updtstatus;

Now your JSON is in fact something like this {"count":"true"}. So change your if statement slightly.

   $.getJSON("check_time.php", function(update) {
        if (update.count===true) {
        } else {
            console.log("No results");

This fiddle simulates the above answer