How to Create an Auto Complete Search using jQuery with PHP/MySQLi

Getting Started

First, we need the jQuery library and Bootstrap for a better design to our app. I've included these files in the downloadable of this tutorial but if you want, you can download them yourself using the links below: For Bootstrap For jQuery

Creating our Database

Next, we create the database that we are going to filter in this tutorial. I've included a SQL file in the downloadable of this tutorial. All you have to do is import the said file. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database. You should be able to create a database named mydb.

Creating our Search Form

Next, we create our search form where we search for a keyword. Create a new file, name it as index.html and paste the codes below.
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <title>How to Create an Auto Complete Search using jQuery with PHP/MySQLi</title>
  4. <link rel="stylesheet" type="text/css" href="bootstrap4/css/bootstrap.min.css">
  5. <style type="text/css">
  6. .mt20{
  7. margin-top:20px;
  8. }
  9. #autocomplete{
  10. display: none;
  11. }
  12. .list-group-item a{
  13. text-decoration: none !important;
  14. }
  15. .list-group li{
  16. cursor: pointer;
  17. }
  18. .list-group li:hover{
  19. color: white;
  20. background-color: #428bca;
  21. }
  22. </style>
  23. </head>
  24. <div class="container">
  25. <h1 class="text-center mt20">Auto Complete Search using jQuery</h1>
  26. <div class="row justify-content-center mt20">
  27. <div class="col-sm-6">
  28. <form id="autoForm" method="POST" action="">
  29. <div class="input-group input-group-lg">
  30. <input type="text" id="keyword" name="keyword" class="form-control" placeholder="Firstname, Lastname or Nickname" aria-describedby="basic-addon2">
  31. <div class="input-group-append">
  32. <button class="btn btn-outline-secondary" type="submit">Search</button>
  33. </div>
  34. </div>
  35. </form>
  36. <ul class="list-group" id="autocomplete"></ul>
  37. </div>
  38. </div>
  39. </div>
  40.  
  41. <script src="jquery.min.js"></script>
  42. <script src="app.js"></script>
  43. </body>
  44. </html>

Creating our jQuery Script

Next, we create our jQuery script where we put all our jquery functions and request. Create a new file, name it as app.js and paste the codes below.
  1. $(document).ready(function(){
  2. //generate suggestion on keyup
  3. $('#keyword').keyup(function(e){
  4. e.preventDefault();
  5. var form = $('#autoForm').serialize();
  6. $.ajax({
  7. type: 'POST',
  8. url: 'search.php',
  9. data: form,
  10. dataType: 'json',
  11. success: function(response){
  12. if(response.error){
  13. $('#autocomplete').hide();
  14. }
  15. else{
  16. $('#autocomplete').show().html(response.data);
  17. }
  18. }
  19. });
  20. });
  21.  
  22. //fill the input
  23. $(document).on('click', '.list-group-item', function(e){
  24. e.preventDefault();
  25. $('#autocomplete').hide();
  26. var fullname = $(this).data('fullname');
  27. $('#keyword').val(fullname);
  28. });
  29. });

Creating our Search Script

Lastly, we create the script that searches our database from the inputted keyword. Create a new file, name it as search.php and paste the codes below.
  1. <?php
  2. if(isset($_POST['keyword'])){
  3. //connection
  4. $conn = new mysqli('localhost', 'root', '', 'mydb');
  5.  
  6. //initialize output
  7. $output = array('error' => false, 'data' => '');
  8.  
  9. $keyword = $_POST['keyword'];
  10.  
  11. //check if keyword is empty
  12. if(empty($keyword)){
  13. $output['error'] = true;
  14. }
  15. else{
  16. //getting data from database via keyword
  17. $sql = "SELECT * FROM members WHERE firstname LIKE '%$keyword%' OR lastname LIKE '%$keyword%' OR nickname LIKE '%$keyword%'";
  18. $query = $conn->query($sql);
  19.  
  20. if($query->num_rows > 0){
  21. while($row = $query->fetch_assoc()){
  22. $output['data'] .= "
  23. <li class='list-group-item' data-fullname='".$row['firstname']." ".$row['lastname']."'>".$row['firstname']." ".$row['lastname']."</li>
  24. ";
  25. }
  26. }
  27. else{
  28. $output['data'] = "
  29. <li class='list-group-item'>No data matches keyword</li>
  30. ";
  31. }
  32. }
  33.  
  34. echo json_encode($output);
  35. }
  36. ?>
That ends this tutorial. Happy Coding :)

Add new comment