PHP - Simple Autocomplete Textbox Using jQuery

In this tutorial we will create a Simple Autocomplete Textbox using jQuery. This code will populate the textbox when the user enter some keyword. The code use AJAX script to fetch the list of data and display dynamically without refreshing the web page. This is a user-friendly kind of program feel free to modify it and use it as your own. We will be using jQuery to simplify and the client-side scripting of HTML. The use of jQuery is to provide an easy way to use JavaScript and Ajax API that works on a lot of different type of browsers. .

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_highlight, 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 = mysqli_connect("localhost", "root", "", "db_autocomplete");
  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. <style>
  7. #list {
  8. list-style:none;
  9. margin:0;
  10. padding:0;
  11. color:#000;
  12. border:1px solid #ccc;
  13. width:67%;
  14. margin-right:0px;
  15. }
  16. #list li {
  17. padding: 10px;
  18. font-size:15px;
  19. background:#FAFAFA;
  20. border-bottom:1px solid #ccc;
  21. }
  22. #list li:hover {
  23. background:#eee;
  24. cursor:pointer;
  25. font-size:18px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <nav class="navbar navbar-default">
  31. <div class="container-fluid">
  32. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  33. </div>
  34. </nav>
  35. <div class="col-md-3"></div>
  36. <div class="col-md-6 well">
  37. <h3 class="text-primary">PHP - Simple Autocomplete Textbox Using jQuery</h3>
  38. <hr style="border-top:1px dotted #ccc;"/>
  39. <div class="col-md-3"></div>
  40. <div class="col-md-6" style="height:300px;">
  41. <div class="form-inline">
  42. <label>Search Country:</label>
  43. <input type="text" id="searchbox" class="form-control" />
  44. <button type="button" id="search" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search</button>
  45. <div id="country_list"></div>
  46. </div>
  47. </div>
  48. </div>
  49. <script src="js/jquery-3.2.1.min.js" type="text/javascript"></script>
  50. <script src="js/script.js"></script>
  51. </body>
  52. </html>

Creating the Main Function

This code contains the main function of the application. This code will populate the textbox when a character is inputted. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. countries.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(!empty($_POST['keyword'])){
  5. $query = mysqli_query($conn, "SELECT * FROM `country` WHERE `countries` LIKE '%".$_POST['keyword']."%' LIMIT 0, 5") or die(mysqli_error());
  6. ?>
  7. <ul id="list">
  8. <?php
  9. while($fetch = mysqli_fetch_array($query)){
  10. ?>
  11. <li onClick="displayCountry('<?php echo $fetch["countries"]; ?>');"><?php echo $fetch["countries"]; ?></li>
  12. <?php
  13. }
  14. ?>
  15. </ul>
  16. <?php
  17. }
  18. ?>
script.js Note: Make sure you save this file inside the js directory to make this work.
  1. $(document).ready(function() {
  2. $('.table-row').hover(function() {
  3. $(this).addClass('highlight-row');
  4. }, function() {
  5. $(this).removeClass('highlight-row');
  6. });
  7.  
  8. $("th").hover(function() {
  9. var index = $(this).index();
  10. $(this).css('cursor', 'pointer');
  11. $("th.header, td").filter(":nth-child(" + (index+1) + ")").addClass("highlight-col");
  12. $("th.header").filter(":nth-child(" + (index+1) + ")").attr('class', 'alert-info');
  13. }, function() {
  14. var index = $(this).index();
  15. $("th.header, td").removeClass("highlight-col");
  16. });
  17. });
There you have it we successfully created Simple Autocomplete Textbox 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