PHP - Simple Search Using Ajax & MySQLi

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

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="<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 Search Using Ajax/MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST">
  19. <div class="form-inline">
  20. <input type="text" id="search_data" class="form-control" placeholder="Search here..."/>
  21. <button type="button" id="search" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search</button>
  22. <button type="button" id="refresh" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span></button>
  23. </div>
  24. </form>
  25. <br /><br />
  26. <table class="table table-bordered">
  27. <thead class="alert-success">
  28. <tr>
  29. <th>Firstname</th>
  30. <th>Lastname</th>
  31. <th>Address</th>
  32. <th>Gender</th>
  33. <th>Age</th>
  34. </tr>
  35. </thead>
  36. <tbody class="alert-warning" id="data"></tbody>
  37. </table>
  38. </div>
  39. </body>
  40. <script src="js/jquery-3.2.1.min.js"></script>
  41. <script type="text/javascript">
  42. $(document).ready(function(){
  43. DisplayData();
  44.  
  45. $('#search').on('click', function(){
  46. if($('#search_data').val() == ""){
  47. alert("Please enter something first!");
  48. }else{
  49. var search = $('#search_data').val();
  50. var loader = $('<tr ><td colspan = "5"><center>Searching....</center></td></tr>');
  51. $('#data').empty();
  52. loader.appendTo('#data');
  53.  
  54. setTimeout(function(){
  55. loader.remove();
  56. $.ajax({
  57. url: 'search.php',
  58. type: 'POST',
  59. data: {
  60. search: search
  61. },
  62. success: function(data){
  63. $('#data').html(data);
  64. }
  65. });
  66.  
  67. }, 3000);
  68. }
  69. });
  70.  
  71. $('#refresh').on('click', function(){
  72. DisplayData();
  73. });
  74.  
  75.  
  76. function DisplayData(){
  77. $.ajax({
  78. url: 'data.php',
  79. type: 'POST',
  80. data: {
  81. res: 1
  82. },
  83. success: function(data){
  84. $('#data').html(data);
  85. }
  86. });
  87. }
  88. });
  89. </script>
  90. </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

Pls I need your help, am new in programming and I need you help(guide) sir, contact on [email protected].. Thanks

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.