PHP - Auto Search Using jQuery

In this tutorial we will create a Auto Search using PHP. This code will auto search a specific data in the MySQLi server when the user entered a keyword. The code use jQuery ajax to automatically search a table row data from MySQLi server without refreshing web page and modify HTML table to display the result. This is a user-friendly kind of program feel free to modify it and use it as your own. We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can retrieve DOM and manipulate each properties of an element based on different criteria such as id, class, etc. .

Getting 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 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_auto, 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(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = new mysqli('localhost', 'root', '', 'db_auto');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Cam't connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <head>
  5.                         <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.                         <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7.                 </head>
  8.         </head>
  9. <body>
  10.         <nav class="navbar navbar-default">
  11.                 <div class="container-fluid">
  12.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  13.                 </div>
  14.         </nav>
  15.         <div class="col-md-3"></div>
  16.         <div class="col-md-6 well">
  17.                 <h3 class="text-primary">PHP - Auto Search Using jQuery</h3>
  18.                 <hr style="border-top:1px dotted #ccc;"/>
  19.                 <div class="col-md-4">
  20.                         <form action="save.php" method="POST">
  21.                                 <div class="form-group">
  22.                                         <label>Firstname</label>
  23.                                         <input type="text" class="form-control" name="firstname" required="required"/>
  24.                                 </div>
  25.                                 <div class="form-group">
  26.                                         <label>Lastname</label>
  27.                                         <input type="text" class="form-control" name="lastname" required="required"/>
  28.                                 </div>
  29.                                 <div class="form-group">
  30.                                         <label>Address</label>
  31.                                         <input type="text" class="form-control" name="address" required="required"/>
  32.                                 </div>
  33.                                 <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Add Member</button></center>
  34.                         </form>
  35.                        
  36.                 </div>
  37.                 <div class="col-md-8">
  38.                         <div class="form-group">
  39.                                 <input class="form-control" type="text" id="search" placeholder="Search here..."/>
  40.                         </div>
  41.                         <div id="result"></div>
  42.                 </div>
  43.         </div>
  44. </body>
  45. <script src="js/jquery-3.2.1.min.js"></script>
  46. <script src="js/bootstrap.js"></script>
  47. <script src="js/script.js"></script>
  48. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data inputs to the database server. To do this just copy and write these block of codes below inside the text editor, then save it as save.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                
  6.                 $firstname = $_POST['firstname'];
  7.                 $lastname = $_POST['lastname'];
  8.                 $address = $_POST['address'];
  9.                
  10.                 $conn->query("INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_errno());
  11.                
  12.                 header('location: index.php');
  13.  
  14.         }
  15. ?>

Creating the Main Function

This code contains the main function of the application. This code will auto search a specific data in the database when the keyword is entered. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. auto_search.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['search'])){
  5.                 $query = $conn->query("SELECT * FROM `member` WHERE (`firstname` LIKE '%".$_POST['search']."%') OR (`lastname` LIKE '%".$_POST['search']."%') OR (`address` LIKE '%".$_POST['search']."%') ");
  6.                
  7.                 $row = $query->num_rows;
  8.                
  9.                 if($row > 0){
  10.                         $output = "";
  11.                                 $output .= "
  12.                         <center><h3>Search Result</h3></center>
  13.                         <table class='table'>
  14.                                 <thead>
  15.                                         <tr>
  16.                                                 <th>Firstname</th>
  17.                                                 <th>Lastname</th>
  18.                                                 <th>Address</th>
  19.                                         </tr>
  20.                                 </thead>
  21.                                 <tbody>
  22.                         ";
  23.                        
  24.                         while($fetch = $query->fetch_array()){
  25.                                 $output .= "
  26.                                         <tr>
  27.                                                 <td>".$fetch['firstname']."</td>
  28.                                                 <td>".$fetch['lastname']."</td>
  29.                                                 <td>".$fetch['address']."</td>
  30.                                         </tr>
  31.                                 ";
  32.                         }
  33.                        
  34.                         $output .="
  35.                                 </tbody>
  36.                         </table>
  37.                         ";
  38.                        
  39.                         echo $output;
  40.                 }else{
  41.                         echo "<center><h4>Search Not Found!</h4></center>";
  42.                 }
  43.                        
  44.         }
  45. ?>
script.js Note: To make the script works make sure you save this file inside the js directory.
  1. $(document).ready(function(){
  2.         $('#search').on('keyup', function(){
  3.                 if($("#search").val() == ""){
  4.                         $('#result').empty();
  5.                 }else{
  6.                         var search = $("#search").val();
  7.                         $.ajax({
  8.                                 url: 'auto_search.php',
  9.                                 type: 'POST',
  10.                                 data: {search: search},
  11.                                 dataType: 'text',
  12.                                 success: function(data){
  13.                                         $("#result").html(data);
  14.                                 }
  15.                         });
  16.                 }
  17.         });
  18. });
There you have it we successfully created Auto Search 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!

Add new comment