PHP - Auto Search Using jQuery

Submitted by: 
Language: 
Visitors have accessed this post 120 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  13. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  14. </a> </div>
  15. </nav>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6 well">
  18. <h3 class="text-primary">PHP - Auto Search Using jQuery</h3>
  19. <hr style="border-top:1px dotted #ccc;"/>
  20. <div class="col-md-4">
  21. <form action="save.php" method="POST">
  22. <div class="form-group">
  23. <label>Firstname</label>
  24. <input type="text" class="form-control" name="firstname" required="required"/>
  25. </div>
  26. <div class="form-group">
  27. <label>Lastname</label>
  28. <input type="text" class="form-control" name="lastname" required="required"/>
  29. </div>
  30. <div class="form-group">
  31. <label>Address</label>
  32. <input type="text" class="form-control" name="address" required="required"/>
  33. </div>
  34. <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Add Member</button></center>
  35. </form>
  36.  
  37. </div>
  38. <div class="col-md-8">
  39. <div class="form-group">
  40. <input class="form-control" type="text" id="search" placeholder="Search here..."/>
  41. </div>
  42. <div id="result"></div>
  43. </div>
  44. </div>
  45. </body>
  46. <script src="js/jquery-3.2.1.min.js"></script>
  47. <script src="js/bootstrap.js"></script>
  48. <script src="js/script.js"></script>
  49. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.