Simple Autocomplete in PHP/MySQLi & jQuery UI Tutorial

In this tutorial we will create a Simple Autocomplete With MySQLi & jQuery using PHP. This code can be used for searching some data from database and some system that uses a large data. 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.

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. After the Installation, open the XAMPP's Control Panel and start Apache and MySQL.

And this is the link for the jquery that i used in this tutorial https://jquery.com/

Lastly, this is the link for the jquery ui that i used for the autocomplete function https://jqueryui.com/

Creating Database

Open your database web server then create a database naming db_text. Then go to the SQL tab and copy/paste the script below and click the GO button below.

  1. CREATE TABLE `product` (
  2.   `product_id` int(11) NOT NULL,
  3.   `product` varchar(50) NOT NULL
  4.  
  5. INSERT INTO `product` (`product_id`, `product`) VALUES
  6. (1, 'Shampoo'),
  7. (2, 'Soap'),
  8. (3, 'Milk'),
  9. (4, 'Wheat'),
  10. (5, 'Rice'),
  11. (6, 'Apple'),
  12. (7, 'Pork'),
  13. (8, 'Chicken');
  14.  
  15. ALTER TABLE `product`
  16. ADD PRIMARY KEY (`product_id`);
  17.  
  18. ALTER TABLE `product`

Or, you can also import the provided SQL File provided along with the source code zip file inside the database folder. Click the Import Tab Menu in PHPMyAdmin and choose the sql file 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_text');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Can't 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 you text editor, then save it as shown below

. index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/jquery-ui.css"/>
  6.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  7.         </head>
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Simple Autocomplete With MySQLi & jQuery</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-2"></div>
  19.                 <div class="col-md-8">
  20.                         <div class="form-group">
  21.                                 <form action="" method="POST">
  22.                                         <label>Search Product here...</label>
  23.                                         <div class="auto-widget">
  24.                                                 <input class="form-control" id="search" type="text" name="product"/>
  25.                                         </div>
  26.                                 </form>
  27.                         </div>
  28.                 </div>
  29.         </div>
  30. </body>
  31. <script src="js/jquery-3.2.1.min.js"></script>
  32. <script src="js/jquery-ui.js"></script>
  33. <script type="text/javascript">
  34.         $(document).ready(function(){
  35.                 $("#search").autocomplete({
  36.                         source: 'search.php',
  37.                         minLength: 0,
  38.                 });
  39.         });
  40. </script>
  41. </html>

Creating the Main Function

This code contains the specific script for the autocomplete. This code will send request to the database then return it as a javascript array to display in the textbox input. To do that write these block of codes inside the Text editor and call it as search.php.

  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         $search = $_GET['term'];
  5.        
  6.         $query = $conn->query("SELECT * FROM `product` WHERE `product` LIKE '%$search%' ORDER BY `product` ASC") or die(mysqli_connect_errno());
  7.        
  8.         $list = array();
  9.         $rows = $query->num_rows;
  10.        
  11.         if($rows > 0){
  12.                 while($fetch = $query->fetch_assoc()){
  13.                         $data['value'] = $fetch['product'];
  14.                         array_push($list, $data);
  15.                 }
  16.         }
  17.        
  18.         echo json_encode($list);
  19. ?>

DEMO

There you have it we successfully created a Simple Autocomplete With MySQLi & jQuery using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!!

Add new comment