I'm developing a dashboard, it've a notifications also so i planned for a live notifications with ajax,php,mysql.i've made it possible but the mysqli(from php) reads is slow for fetch results from database,below is my code
this is my javascript code on index.php:
comres = {
connect: function() {
return $.ajax({
url: 'read_notification.php',
type: 'POST',
success: function(evt, request) {
alert(evt);
},
complete: function() {
comres.connect();
}
});
}
}
$(document).ready(function() {
comres.connect();
});
below is my read_notification.php
<?php
require_once("db.php");
$query=mysqli_query($db,"select * from notifications");
while(mysqli_num_rows($query) < 1)
{
}
$query1 = mysqli_query($db,"select * from notifications");
$row = mysqli_fetch_array($query1);
$id=$row['id'];
$notification=$row['notification'];
echo "you have a notification ".$notification
mysqli_query($db,"delete from ticket.handover_response where id='$id'");
?>
below is working flow
read_notification.php
when loads the index.php
here no problem is in working flow but i've a delay on after have a record on database(php is not reading suddenly from database,it's working after few more seconds), please suggest the reason or solution for this, thanks
Try this
$(document).ready(function() {
setInterval(function(){
//set up an ajax request every 2 sec
comres.connect();
},2000);
});
In your php simply display new notification based on some flag variable.that is if user click the notification set the flag to one (dont show again)otherwise mark it as zero.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.