Skip to content

Instantly share code, notes, and snippets.

@Mauryashubham
Created March 10, 2017 07:13
Show Gist options
  • Save Mauryashubham/d5329a7d4d96fe9bddf9986149ff45ad to your computer and use it in GitHub Desktop.
Save Mauryashubham/d5329a7d4d96fe9bddf9986149ff45ad to your computer and use it in GitHub Desktop.
Simple Live Notification Example in PHP using Ajax
Hi all , Welcome to Maurya Tricks , Today we are going to discuss ,
how to make a Simple Live Notification Example in PHP using Ajax
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/
//Database Creation
CREATE TABLE `messagetest` (
`id` int(50) NOT NULL,
`notification` varchar(255) NOT NULL,
`status` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
1.Make a file in notepad and save it as index.php and paste the below code.
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
#notification_count
{
padding: 0px 3px 3px 7px;
background: #cc0000;
color: #ffffff;
font-weight: bold;
margin-left: 77px;
border-radius: 9px;
-moz-border-radius: 9px;
-webkit-border-radius: 9px;
position: absolute;
font-size: 15px;
z-index: 1;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
$('#notification_count').html(msg);
}
function cutmsg(type, msg1){
$('#notification_count').html(msg1);
}
function removeNotification(){
$.ajax({
type: "GET",
url: "remove.php",
async: true,
cache: false,
timeout:50000,
success: function(data){
cutmsg("new", data);
setTimeout(
waitForMsg,
1000
);
}
});
}
function waitForMsg(){
$.ajax({
type: "GET",
url: "select.php",
async: true,
cache: false,
timeout:50000,
success: function(data){
addmsg("new", data);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg,
15000);
}
});
};
$(document).ready(function(){
waitForMsg();
});
</script>
</head>
<body>
<span id="notification_count"></span>
<a href="#" id="notificationLink" onclick = "removeNotification()"><i class="fa fa-bell" aria-hidden="true" style="position: relative;left: 60px;font-size: 30px;"></i></a>
<div id="HTMLnoti" style="text-align:center"></div>
</body>
</html>
1.Make a file in notepad and save it as remove.php and paste the below code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE messageTest SET status = 'read' where status = 'unread'";
if ($conn->query($sql) === TRUE) {
// echo "No Notification";
} else {
// echo "Error updating record: " . $conn->error;
}
$sql1 = "SELECT * from messageTest where status = 'unread'";
$result1 = $conn->query($sql1);
$row1 = $result1->fetch_assoc();
$count1 = $result1->num_rows;
echo $count1;
$conn->close();
?>
1.Make a file in notepad and save it as select.php and paste the below code.<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT * from messageTest where status = 'unread'";
$result = $conn->query($sql1);
$row = $result->fetch_assoc();
$count = $result->num_rows;
echo $count;
$conn->close();
?>
@navinxnx
Copy link

dfvfdbd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment