PHP - Simple Live Search Using jQuery

In this tutorial we will create a Simple Live Search using jQuery. By using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. So Let's do the coding...

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 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_searchafter 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 = mysqli_connect("localhost", "root", "", "db_search");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to 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. <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 Live Search Using jQuery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add member</button>
  18. <div class="form-inline pull-right">
  19. <input type="text" id="search" class="form-control" placeholder="Search here..."/>
  20. </div>
  21. <br /><br />
  22. <table class="table table-bordered">
  23. <thead class="alert-info">
  24. <tr>
  25. <th>Firstname</th>
  26. <th>Lastname</th>
  27. <th>Gender</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;" id="result"></tbody>
  31. </table>
  32. </div>
  33. <div class="modal fade" id="form_modal" aria-hidden="true">
  34. <div class="modal-dialog">
  35. <div class="modal-content">
  36. <form method="POST" action="save_member.php">
  37. <div class="modal-header">
  38. <h3 class="modal-title">Add Member</h3>
  39. </div>
  40. <div class="modal-body">
  41. <div class="col-md-2"></div>
  42. <div class="col-md-8">
  43. <div class="form-group">
  44. <label>Firstname</label>
  45. <input type="text" name="firstname" class="form-control" required="required"/>
  46. </div>
  47. <div class="form-group">
  48. <label>Lastname</label>
  49. <input type="text" name="lastname" class="form-control" required="required" />
  50. </div>
  51. <div class="form-group">
  52. <label>Gender</label>
  53. <select name="gender" class="form-control" required="required">
  54. <option value="">Please select an option</option>
  55. <option value="Male">Male</option>
  56. <option value="Female">Female</option>
  57. </select>
  58. </div>
  59. </div>
  60. </div>
  61. <div style="clear:both;"></div>
  62. <div class="modal-footer">
  63. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  64. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  65. </div>
  66. </div>
  67. </form>
  68. </div>
  69. </div>
  70. </div>
  71. <script src="js/jquery-3.2.1.min.js"></script>
  72. <script src="js/bootstrap.js"></script>
  73. <script src="js/script.js"></script>
  74. </body>
  75. </html>

Creating PHP Query

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

Creating the Main Function

This code contains the main function of the application. This code will search the target keyword in the database without clicking a button. To do this just copy the block of codes inside the text editor, then save it as show below. data.php
  1. <?php
  2. require 'conn.php';
  3.  
  4. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  5. while($fetch = mysqli_fetch_array($query)){
  6. ?>
  7. <tr>
  8. <td><?php echo $fetch['firstname']?></td>
  9. <td><?php echo $fetch['lastname']?></td>
  10. <td><?php echo $fetch['gender']?></td>
  11. </tr>
  12. <?php
  13. }
  14. ?>
search.php
  1. <?php
  2. require 'conn.php';
  3.  
  4. $keyword = $_POST['keyword'];
  5.  
  6. $query = mysqli_query($conn, "SELECT * FROM `member` WHERE `firstname` LIKE '%$keyword%' OR `lastname` LIKE '%$keyword%' OR `gender` LIKE '%$keyword%'") or die(mysqli_error());
  7. while($fetch = mysqli_fetch_array($query)){
  8. ?>
  9. <tr>
  10. <td><?php echo $fetch['firstname']?></td>
  11. <td><?php echo $fetch['lastname']?></td>
  12. <td><?php echo $fetch['gender']?></td>
  13. </tr>
  14. <?php
  15. }
  16. ?>
script.js
  1. $(document).ready(function(){
  2.  
  3. getData();
  4.  
  5. $('#search').on('keyup', function(){
  6. var keyword = $(this).val();
  7. if(keyword === ""){
  8. getData();
  9. }else{
  10. search(keyword);
  11. }
  12. });
  13.  
  14. });
  15.  
  16.  
  17. function getData(){
  18. $.ajax({
  19. url: 'data.php',
  20. type: 'POST',
  21. data: {res: 1},
  22. success: function(data){
  23. $('#result').html(data);
  24. }
  25. });
  26. }
  27.  
  28. function search(searchText){
  29. $.ajax({
  30. url: 'search.php',
  31. type: 'POST',
  32. data: {keyword: searchText},
  33. success: function(data){
  34. $('#result').html(data);
  35. }
  36. });
  37. }
Note: The script must be save inside the js directory to make this work. There you have it we successfully created Simple Live Search using jQuery. 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