Charles Tester Charles Tester - 6 months ago 14
jQuery Question

AJAX get() data

I have a block of jQuery which uses the

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

Jquery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
setInterval(function() {
$.getJSON("check_time.php", function(update) {
if (update) {
$("#slideshow").load("phppage.php");
}
});
}, 600000);
</script>


PHP - check_time.php

<?php
require_once('connect_pdo.php');
header('Content-type: application/json');
$stmt = $conn->prepare("$sqlst = $conn->prepare("SELECT COUNT(*) AS count
FROM ads
WHERE lastupdate > NOW() - INTERVAL 10 MINUTE");
$sqlst->execute();
$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).

Answer

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) {
            $("#slideshow").load("phppage.php");
        } else {
            console.log("No results");
        }
    });

This fiddle simulates the above answer

Comments