I have a jQuery script running that makes a periodic AJAX call using the following code.
var a = moment();
var dayOfMonth = a.format("MMM Do");
var timeSubmitted = a.format("h:mm a");
var count_cases = -1;
var count_claimed = -1;
setInterval(function(){
//check if new lead was added to the db
$.ajax({
type : "POST",
url : "inc/new_lead_alerts_process.php",
dataType: 'json',
cache: false,
success : function(response){
$.getJSON("inc/new_lead_alerts_process.php", function(data) {
if (count_cases != -1 && count_cases != data.count) {
window.location = "new_lead_alerts.php?id="+data.id;
}
count_cases = data.count;
});
}
});
This is the PHP that runs with each call:
$count = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM leads"));
$client_id = mysql_fetch_array(mysql_query("SELECT id, client_id FROM leads ORDER BY id DESC LIMIT 1"));
echo json_encode(array("count" => $count['count'], "id" => $client_id['id'], "client_id" => $client_id['client_id']));
I need to change the code so that the alert only triggers when a new entry is added to the database, not when an existing entry is removed. As it stands, the alert fires on both events.
Any help is greatly appreciated.
The solution was very simple. I changed
count_cases != data.counttocount_cases < data.count. That was all there was to it.