PHP - Simple Search Using Ajax & MySQLi

In this tutorial we will create a Simple Search Using Ajax&MySQLi using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. Ajax is a client-side script that communicates to and from a server/database without the need for a postback or a complete page refresh. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_search, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_search');
  3.  
  4. if(!$conn){
  5. die("Error: Can't connect to the database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create the appearance of an application. To create this simply copy and write this block of code inside the text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Search Using Ajax/MySQLi</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST">
  18. <div class="form-inline">
  19. <input type="text" id="search_data" class="form-control" placeholder="Search here..."/>
  20. <button type="button" id="search" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search</button>
  21. <button type="button" id="refresh" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span></button>
  22. </div>
  23. </form>
  24. <br /><br />
  25. <table class="table table-bordered">
  26. <thead class="alert-success">
  27. <tr>
  28. <th>Firstname</th>
  29. <th>Lastname</th>
  30. <th>Address</th>
  31. <th>Gender</th>
  32. <th>Age</th>
  33. </tr>
  34. </thead>
  35. <tbody class="alert-warning" id="data"></tbody>
  36. </table>
  37. </div>
  38. </body>
  39. <script src="js/jquery-3.2.1.min.js"></script>
  40. <script type="text/javascript">
  41. $(document).ready(function(){
  42. DisplayData();
  43.  
  44. $('#search').on('click', function(){
  45. if($('#search_data').val() == ""){
  46. alert("Please enter something first!");
  47. }else{
  48. var search = $('#search_data').val();
  49. var loader = $('<tr ><td colspan = "5"><center>Searching....</center></td></tr>');
  50. $('#data').empty();
  51. loader.appendTo('#data');
  52.  
  53. setTimeout(function(){
  54. loader.remove();
  55. $.ajax({
  56. url: 'search.php',
  57. type: 'POST',
  58. data: {
  59. search: search
  60. },
  61. success: function(data){
  62. $('#data').html(data);
  63. }
  64. });
  65.  
  66. }, 3000);
  67. }
  68. });
  69.  
  70. $('#refresh').on('click', function(){
  71. DisplayData();
  72. });
  73.  
  74.  
  75. function DisplayData(){
  76. $.ajax({
  77. url: 'data.php',
  78. type: 'POST',
  79. data: {
  80. res: 1
  81. },
  82. success: function(data){
  83. $('#data').html(data);
  84. }
  85. });
  86. }
  87. });
  88. </script>
  89. </html>

Creating PHP functions

This code contains the main function of the application. This code will send request to the database server by using ajax, then return the string that has been search via a php query. To do that copy and write these block of codes inside your text editor, then save it as shown below. data.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['res'])){
  5. $query = $conn->query("SELECT * FROM `member` ORDER BY `lastname` ASC");
  6. while($fetch = $query->fetch_array()){
  7. echo "
  8. <tr>
  9. <td>".$fetch['firstname']."</td>
  10. <td>".$fetch['lastname']."</td>
  11. <td>".$fetch['address']."</td>
  12. <td>".$fetch['gender']."</td>
  13. <td>".$fetch['age']."</td>
  14. </tr>
  15. ";
  16. }
  17. }
  18. ?>
search.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['search'])){
  5. $search = $_POST['search'];
  6. $query = $conn->query("SELECT * FROM `member` WHERE (`lastname` LIKE '%".$search."%') OR (`address` LIKE '%".$search."%') ORDER BY `lastname` ASC");
  7. $rows = $query->num_rows;
  8.  
  9. if($rows > 0){
  10. while($fetch = $query->fetch_array()){
  11. echo "
  12. <tr>
  13. <td>".$fetch['firstname']."</td>
  14. <td>".$fetch['lastname']."</td>
  15. <td>".$fetch['address']."</td>
  16. <td>".$fetch['gender']."</td>
  17. <td>".$fetch['age']."</td>
  18. </tr>
  19. ";
  20. }
  21. }else{
  22. echo "
  23. <tr>
  24. <td colspan='5'><center>No Search Found!</center></td>
  25. </tr>
  26. ";
  27. }
  28. }
  29. ?>

Creating The Ajax Funcition

This is where the code that uses ajax script. This code contains several functionalities that need to send request to the php server. To do that just simply copy and write this block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2. DisplayData();
  3.  
  4. $('#search').on('click', function(){
  5. if($('#search_data').val() == ""){
  6. alert("Please enter something first!");
  7. }else{
  8. var search = $('#search_data').val();
  9. var loader = $('<tr ><td colspan = "5"><center>Searching....</center></td></tr>');
  10. $('#data').empty();
  11. loader.appendTo('#data');
  12.  
  13. setTimeout(function(){
  14. loader.remove();
  15. $.ajax({
  16. url: 'search.php',
  17. type: 'POST',
  18. data: {
  19. search: search
  20. },
  21. success: function(data){
  22. $('#data').html(data);
  23. }
  24. });
  25.  
  26. }, 3000);
  27. }
  28. });
  29.  
  30. $('#refresh').on('click', function(){
  31. DisplayData();
  32. });
  33.  
  34.  
  35. function DisplayData(){
  36. $.ajax({
  37. url: 'data.php',
  38. type: 'POST',
  39. data: {
  40. res: 1
  41. },
  42. success: function(data){
  43. $('#data').html(data);
  44. }
  45. });
  46. }
  47. });
There you have it we successfully created a Simple Search Using Ajax&MySQLi using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Comments

Submitted byKazeem Sodiq (not verified)on Sun, 06/10/2018 - 02:40

Pls I need your help, am new in programming and I need you help(guide) sir, contact on [email protected].. Thanks
Submitted byafridi3746 (not verified)on Thu, 08/13/2020 - 16:48

Script Not working Correctly

Add new comment