Autocomplete Search Box / textbox in PHP MySQL

Today, We want to share with you autocomplete search box in php mysql.In this post we will show you global search in php mysql with examples, hear for autocomplete textbox with multiple values using jquery, php and mysql we will give you demo and example for implement.In this post, we will learn about How to create an suggestions search box in jQuery and PHP? with an example.

php autocomplete textbox from database example

There are the Following The simple About jquery autocomplete ajax json php Full Information With Example and source code.

As I will cover this Post with live Working example to develop php mysql ajax search autocomplete, so the autofill html form from database is used for this example is following below.

Read Also:  JQuery Ajax Dynamic Treeview using PHP MySQL Example

Step 1: Create a Database Connection File

<?php
    $servername='localhost';
    $username='root';
    $password='';
    $dbname = "shops";
    $conn=mysqli_connect($servername,$username,$password,"$dbname");
      if(!$conn){
          die('Could not Connect MySql Server:' .mysql_error());
        }
?>

Step 2: Create an Autocomplete search form

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>suggestions Search Box in PHP MySQL - Pakainfo.com</title>
 
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
 
  <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
  <!-- Bootstrap Css -->
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body> 
<div class="container">
  <div class="row">
     <h2>Search Here</h2>
     <input type="text" name="search" id="search" placeholder="search here...." class="form-control">  
  </div>
</div>
<script type="text/javascript">
  $(function() {
     $( "#search" ).autocomplete({
       source: 'ajax-db-search.php',
     });
  });
</script>
</body>
</html>

Step 3: Create a PHP Script for Search to DB

<?php
require_once "db.php";
if (isset($_GET['term'])) {
     
   $query = "SELECT * FROM products WHERE name LIKE '{$_GET['term']}%' LIMIT 25";
    $result = mysqli_query($conn, $query);
 
    if (mysqli_num_rows($result) > 0) {
     while ($product = mysqli_fetch_array($result)) {
      $results[] = $product['name'];
     }
    } else {
      $results = array();
    }
    //return json results
    echo json_encode($results);
}
?>

how to implement an autocomplete search box or textbox in PHP MySQL with jQuery Ajax from the database table using jQuery UI Autocomplete JS.

Free Live Chat for Any Issue
Download

I hope you get an idea about autocomplete search box in php mysql.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.