Trigger alert when database entries are added, not when they are removed
Posted
by
Jeremy
on Stack Overflow
See other posts from Stack Overflow
or by Jeremy
Published on 2012-10-25T15:51:05Z
Indexed on
2012/10/26
5:01 UTC
Read the original article
Hit count: 157
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.
© Stack Overflow or respective owner