PHP Notification Alert When A New Record Is Inserted Using AJAX

Bipsmedium
3 min readJul 24, 2021
PHP Notification Alert When A New Record Is Inserted Using AJAX
PHP Notification Alert When A New Record Is Inserted Using AJAX
PHP Notification Alert When A New Record Is Inserted Using AJAX

Hi friends, in this tutorial we will learn how to send a PHP notification alert when a new record is inserted using ajax and jquery. This is almost similar to Facebook-like notifications.

As we all know that the notification is a very essential part of any type of web-based application or any dynamic website. In order to do so, you have to follow the below-given steps one by one.

Also read, How to Get Only Date from Timestamp in PHP

Required steps of PHP notification alert

Step 1:- Create a table in the database.

DDL information of the table

CREATE TABLE `notifications` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`subject` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`comment` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` int(10) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Step 2:- In this step, we will create a PHP file using an HTML form with two fields such as subject and comment, and save the file name as phpnotification.php.

phpnotification.php

Step 3:- In this step, we will send the form values with the help of the AJAX request i.e. without reloading the page using the done() function as shown below

Step 4:- In this step, we will receive the form values from the AJAX request and store them in the table of our database where the information will be stored.

Here, we will not create any separate PHP file for storing the data because we are using the same file for storing the form values with the help of PHP scripts as shown below.

Also, note that we are using a status field so that we can populate the unseen notifications after submitting the data depending on that status.

Step 5:- In this step, we are going to view the unseen notification in the dropdown menu after clicking the bell icon as shown in the HTML form.

Here, we have used the click() function with dropdown menu class and another function called load_unseen_notification() to call the notification information which is not seen yet as shown below.

Step 6:- In this step, we will create a separate PHP file called fetchnotification.php which is used to collect the unseen notifications from the database and return the response back to the AJAX request sent from the function load_unseen_notification() as shown below.

Complete Code:-

Conclusion: I hope this tutorial will help you to understand the PHP notification system. If there is any doubt then please leave a comment below.

--

--

Bipsmedium

Hi, This is Biplab and I am a PHP laravel developer and other open source technologies. I am here to share my experience with the community.