This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

[Cyber Week Savings] Unleash the power of managed cloud hosting at 40% OFF for 4 months. GET THE DEAL

Creating Real Time Notification System in PHP and AJAX

Updated on May 16, 2022

5 Min Read

Ever wonder how social media websites pop up a notification for every action that happens on your timeline?

This real-time notification system keeps track of every action you and your friends do on these social channels. Notifications form a big part of the real-time engagement feature of these platforms. Even you are not online, you could still receive these notifications. A PHP notification system could be easily built using a mix of vanilla PHP and JavaScript. This system provides real-time notification in a PHP powered application.

In this article, I will show you how to create a simple notification system by using PHP and AJAX. I will also use jQuery and Bootstrap. For the purpose of this article, I’m assuming that you have already signed up on Cloudways which provides Best PHP Hosting and has launched a server with the PHP application.

PHP Hosting: Best PHP 7 & PHP 5.6 Web Hosting

Let’s get started.

Import Tables in Database

Cloudways has custom-built MySQL manager and you get a pre-built database with every PHP application. Just move to the Application Access area and launch the database manager.

Now run the following query in the SQL box.


SET time_zone = "+00:00";

CREATE TABLE `comments` (

 `comment_id` int(11) NOT NULL,

 `comment_subject` varchar(250) NOT NULL,

 `comment_text` text NOT NULL,

 `comment_status` int(1) NOT NULL


ALTER TABLE `comments`

 ADD PRIMARY KEY (`comment_id`);

ALTER TABLE `comments`


This query will create a table comments and its four columns ‘comment_id’ ‘comment_subject’ ‘comment_text’ and comment_status. All user comments will be entered in this database and then the notifications will be generated.

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Create Navigation And Form To Display Realtime Notifications

I will use the basic navigation and Bootstrap forms by declaring the CDN. Simple copy and paste the code in the index.php file.

<!DOCTYPE html>



 <title>Notification using PHP Ajax Bootstrap</title>

 <script src=""></script>

 <link rel="stylesheet" href="" />

 <script src=""></script>



 <br /><br />

 <div class="container">

  <nav class="navbar navbar-inverse">

   <div class="container-fluid">

    <div class="navbar-header">

     <a class="navbar-brand" href="#">PHP Notification Tutorial</a>


    <ul class="nav navbar-nav navbar-right">

     <li class="dropdown">

      <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="label label-pill label-danger count" style="border-radius:10px;"></span> <span class="glyphicon glyphicon-bell" style="font-size:18px;"></span></a>

      <ul class="dropdown-menu"></ul>





  <br />

  <form method="post" id="comment_form">

   <div class="form-group">

    <label>Enter Subject</label>

    <input type="text" name="subject" id="subject" class="form-control">


   <div class="form-group">

    <label>Enter Comment</label>

    <textarea name="comment" id="comment" class="form-control" rows="5"></textarea>


   <div class="form-group">

    <input type="submit" name="post" id="post" class="btn btn-info" value="Post" />






Insert New Records in Database

Create a connect.php file to create a database connection. Add the following code in it:


$con = mysqli_connect("localhost", "root", "", "notif");


You might be interested in: How To Connect MySQL Database With PHP Websites

Next, I will create insert.php and simply insert new comments in MySQL through the following code:





$subject = mysqli_real_escape_string($con, $_POST["subject"]);

$comment = mysqli_real_escape_string($con, $_POST["comment"]);

$query = "INSERT INTO comments(comment_subject, comment_text)VALUES ('$subject', '$comment')";

mysqli_query($con, $query);



This code snippet is pretty much self-explanatory. It gets the form values and then pushes them to the database tables.

Related: Adding Push Notification in PHP

Fetch Records and Send to AJAX Call

For this action, create a new file named fetch.php. This will check whether the AJAX view is updated with new comments. If not, it will select unique comments and show them in the notification window. Once the user has seen these notifications, the status would be updated to reflect that these notifications have been reviewed. Here, I will send the $data array to the AJAX call for updating the view.

You might be interested in: Build Live Search Box Using PHP, MySQL And AJAX

Paste the following code in the fetch.php file:




// $con = mysqli_connect("localhost", "root", "", "notif");

if($_POST["view"] != '')

   $update_query = "UPDATE comments SET comment_status = 1 WHERE comment_status=0";
   mysqli_query($con, $update_query);

$query = "SELECT * FROM comments ORDER BY comment_id DESC LIMIT 5";
$result = mysqli_query($con, $query);
$output = '';

if(mysqli_num_rows($result) > 0)

while($row = mysqli_fetch_array($result))


  $output .= '
  <a href="#">
  <strong>'.$row["comment_subject"].'</strong><br />


    $output .= '<li><a href="#" class="text-bold text-italic">No Noti Found</a></li>';

$status_query = "SELECT * FROM comments WHERE comment_status=0";
$result_query = mysqli_query($con, $status_query);
$count = mysqli_num_rows($result_query);

$data = array(
   'notification' => $output,
   'unseen_notification'  => $count

echo json_encode($data);

Submit Form and Update HTML with AJAX

Now comes the interesting part!

In the previous step, I sent the data array, which will be caught by the AJAX response for updating the inner HTML on the navigation bar.

Now, I will create a submit method in jQuery which will validate the input data, and select the latest notification(s), which I have inserted in insert.php. In the next onclick function, I will update the count which will be shown in the Bootstrap’s red pill. Here is the complete code that you need to paste in the index.php.



// updating the view with notifications using ajax

function load_unseen_notification(view = '')






   if(data.unseen_notification > 0)





// submit form and get new records

$('#comment_form').on('submit', function(event){

 if($('#subject').val() != '' && $('#comment').val() != '')


  var form_data = $(this).serialize();









  alert("Both Fields are Required");


// load new notifications

$(document).on('click', '.dropdown-toggle', function(){






}, 5000);



Testing The PHP Notification System

Now it’s time to test the notification system. You will see a window like this:

Enter the subject and the comment, and then submit the form. You will get the new notification (see the following image) when you click the dropdown.



Final Words

Notifications offer a quick view of all the actions performed on the website. You can easily click them in the dropdown list and optionally carry out further actions.

The pros and cons largely depend on the usage and implementation of this method. Advantages include better UX and updating users on time etc. The possible technical disadvantage includes the load on MySQL which can be dealt with by using Optimization Tools and performing Server configuration. 

This article presents a basic real-time notification system in PHP that could be further extended to fit your requirements.

If you find any queries and questions, feel free to comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling. You can email him at [email protected]


Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Unleash The Cloud.
Not Budgets.

For 4 Months +
Up To 30 Free Migrations

Cyber Week

Time Left In Offer
  • 0


  • 0


  • 0


  • 0


40% OFF

On All Plans