I show a notification to my users every time a new event is added to my database. I have a small icon on my top page and a red square to show the number of new events that were added. Now I just want to know what the best way is to display new events without refreshing the page.
Here is my code
<li><i class="fa fa-warning fa-2x"></i>
<?php if ($totalRows_event > 0) { ?>
<span class="label label-danger blink"><?php echo $totalRows_event ?></span>
<?php } ?>
</li>
My PHP query is basically when the total of events is > 0, display a <span>
and the total inside, but this only shows when the page is refreshed or load.
I was looking at an AJAX request like the one below that will display the PHP query result into a inner html.
function testAjax() {
var result = "";
$.ajax({
url: "data.php",
async: false,
success: function (data, textStatus) {
$("#preview").html(data);
},
error: function () {
alert('Not OKay');
}
});
return result;
}
<li>
<i class="fa fa-warning fa-2x"></i>
<span class="label label-danger blink" id="preview"></span>
</li>
But how do I call the function every time a new event is added to my database without refreshing or loading the page? I think using a set interval or delay will slow my page because of frequent server queries so I am looking for other options.