PHP - Simple Autocomplete Textbox Using jQuery

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

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.
https://www.sourcecodester.com/sites/default/files/2019-03-14_22_04_59-l...

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  33. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  34. </a> </div>
  35. </nav>
  36. <div class="col-md-3"></div>
  37. <div class="col-md-6 well">
  38. <h3 class="text-primary">PHP - Simple Autocomplete Textbox Using jQuery</h3>
  39. <hr style="border-top:1px dotted #ccc;"/>
  40. <div class="col-md-3"></div>
  41. <div class="col-md-6" style="height:300px;">
  42. <div class="form-inline">
  43. <label>Search Country:</label>
  44. <input type="text" id="searchbox" class="form-control" />
  45. <button type="button" id="search" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Search</button>
  46. <div id="country_list"></div>
  47. </div>
  48. </div>
  49. </div>
  50. <script src="js/jquery-3.2.1.min.js" type="text/javascript"></script>
  51. <script src="js/script.js"></script>
  52. </body>
  53. </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

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.