Attempting to run a scheduled alert with jquery and JSON. The php file works as expected and returns a correctly JSON encoded array. The jquery works and pops up a dialog box every minute when not checking for the JSON data. But when i add in the JSON code, it stops working and nothing happens.
Disclaimer: I know I should no longer be using mysql and should be using prepared statements instead, and I will begin as soon as this project is finished.
EDIT I fixed the typo and changed $getJSON to $.getJSON, and i posted the php script results.
jQuery:
$(function() {
setInterval(function() {
$.getJSON('includes/popup.php', function(data) {
if(data.firstName == "None") {
$("<div>No Callback</div>").dialog();
}
else {
$("<div>Yes Callback</div>").dialog();
}
})
}, 1000 * 60 * 1);
});
popup.php
<?php
$nowdate = date("Y-m-d");
$nowtime = date("H:i");
$alertSql = "SELECT id, compid, cbdate, cbtime, firstName, lastName FROM contacts WHERE cbdate = '$nowdate' ORDER BY lastName desc";
$alertResult = mysql_query($alertSql, $link);
while($alertRow = mysql_fetch_array($alertResult)){
$cbtime = date("H:i", strtotime($alertRow['cbtime']));
if($cbtime == $nowtime) {
$final_array[] = array("firstName" => $alertRow['firstName'], "lastName" => $alertRow['lastName'], "cbdate" => $alertRow['cbdate'], "cbtime" => $alertRow['cbtime']);
echo json_encode($final_array);
}
else {
$final_array[] = array("firstName" => "None");
echo json_encode($final_array);
}
};
?>
php result:
[{"firstName":"None"}]
or
[{"firstName":"Mickey","lastName":"Mouse","cbdate":"2014-09-22","cbtime":"15:36:00"}]
console.log(data);in callback function & post your resultdatalook like in the response? Can you paste the exact PHP response?