Skip to main content

Creating a Pop-up Notification using PHP, AJAX, and Bootstrap

Screenshot
Body

This tutorial will show you how to create a simple pop-up notification using AJAX/JQuery. In this tutorial, I've set up the notification to appear at the bottom but you can change its position depending on your preference. Also, I've used bootstrap to slightly improve the visuals. We will use Ajax to sent the data of our form upon submission to process with the back-end scripts without leaving or the page.

Note: Scripts and CSS used in this tutorial are hosted.

Creating our Database

The first and most important step in to create our database.

  1. Open PHPMyAdmin.
  2. Click databases, create a database and name it as "notif".
  3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` VARCHAR(10) NOT NULL,
  4. `lastname` VARCHAR(10) NOT NULL,
  5. `seen_status` INT(1) NOT NULL,
  6. PRIMARY KEY(`userid`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
popup

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below.

  1. <?php
  2.  
  3. //MySQLi Procedural
  4. $conn = mysqli_connect("localhost","root","","notif");
  5. if (!$conn) {
  6. die("Connection failed: " . mysqli_connect_error());
  7. }
  8.  
  9. ?>

Creating our Main Page

Next step is to create our main page that contains our add form and the place that the notification will appear. We name this as "index.php".

  1. <?php
  2. include('conn.php');
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang = "en">
  6. <head>
  7. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  8. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  9. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  10. <title>PHP Pop-up Notification using AJAX Bootstrap</title>
  11.  
  12. <style>
  13. #alert_popover
  14. {
  15. display:block;
  16. position:absolute;
  17. bottom:50px;
  18. left:50px;
  19. }
  20. .wrapper {
  21. display: table-cell;
  22. vertical-align: bottom;
  23. height: auto;
  24. width:200px;
  25. }
  26. .alert_default
  27. {
  28. color: #333333;
  29. background-color: #f2f2f2;
  30. border-color: #cccccc;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div style="height:30px;"></div>
  36. <div class="row">
  37. <div class="col-md-3">
  38. </div>
  39. <div class="col-md-6 well">
  40. <div class="row">
  41. <div class="col-lg-12">
  42. <center><h2 class="text-primary">PHP Pop-up Notification using AJAX Bootstrap</h2></center>
  43. <hr>
  44. <div>
  45. <form class="form-inline" method="POST" id="add_form">
  46. <div class="form-group">
  47. <label>Firstname:</label>
  48. <input type="text" name="firstname" id="firstname" class="form-control">
  49. </div>
  50. <div class="form-group">
  51. <label>Lastname:</label>
  52. <input type="text" name="lastname" id="lastname" class="form-control">
  53. </div>
  54. <div class="form-group">
  55. <input type="submit" name="addnew" id="addnew" class="btn btn-primary" value="Add">
  56. </div>
  57. </form>
  58. </div>
  59. </div>
  60. </div><br>
  61. </div>
  62. <div class = "col-md-3">
  63.  
  64. </div>
  65. </div>
  66. <div id="alert_popover">
  67. <div class="wrapper">
  68. <div class="content">
  69.  
  70. </div>
  71. </div>
  72. </div>
  73.  
  74. </body>
  75. <script>
  76. $(document).ready(function(){
  77.  
  78. setInterval(function(){
  79. load_last_notification();
  80. }, 5000);
  81.  
  82. function load_last_notification(){
  83. $.ajax({
  84. url:"fetch.php",
  85. method:"POST",
  86. success:function(data){
  87. $('.content').html(data);
  88. }
  89. })
  90. }
  91.  
  92. $('#add_form').on('submit', function(event){
  93. event.preventDefault();
  94. if($('#firstname').val() != '' && $('#lastname').val() != ''){
  95. var form_data = $(this).serialize();
  96. $.ajax({
  97. url:"addnew.php",
  98. method:"POST",
  99. data:form_data,
  100. success:function(data){
  101. $('#add_form')[0].reset();
  102. }
  103. })
  104. }
  105. else{
  106. alert("Please input data first");
  107. }
  108. });
  109. });
  110. </script>
  111. </html>

Creating our Fetch Code

Next, we create our fetch code that will fetch data from our database and return to us via notification. We name this as "fetch.php".

  1. <?php
  2. //fetch.php;
  3. include('conn.php');
  4. $query=mysqli_query($conn,"select * from `user` where seen_status='0' order by userid desc");
  5. $output = '';
  6. while($row=mysqli_fetch_array($query)){
  7. $output .= '
  8. <div class="alert alert_default">
  9. <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
  10. <p>Firstname: <strong>'.$row['firstname'].'</strong><br>
  11. Lastname: <strong>'.$row['lastname'].'</strong></p>
  12. </div>
  13. ';
  14. }
  15.  
  16. mysqli_query($conn,"update `user` set seen_status='1' where seen_status='0'");
  17. echo $output;
  18.  
  19. ?>

Creating our Add Code

Lastly, we create our add code that will add data into our database. We name this as "addnew.php".

  1. <?php
  2. //insert.php
  3. if(isset($_POST["firstname"]))
  4. {
  5. include('conn.php');
  6. $firstname = mysqli_real_escape_string($conn, $_POST['firstname']);
  7. $lastname = mysqli_real_escape_string($conn, $_POST['lastname']);
  8.  
  9. mysqli_query($conn,"insert into `user` (firstname, lastname) values ('$firstname', '$lastname')");
  10. }
  11. ?>

That's you have now a simple web application that has a notification feature. The notification feature can be useful for any web application especially to backup some features that need to notify users when updated such as messaging features.

I hope this tutorial will help you with what you are looking for and also for your future PHP project development.

Happy Coding :)

Add new comment