PHP - Simple Live Search Using jQuery

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

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

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.