Autocomplete Search Box In Php Mysql


Autocomplete Search, Autocomplete Textbox From Database in PHP Example. today We will learn how to implement an autocomplete search box, textbox in the PHP MySQL database using jQuery.

Here we will implement an autocomplete search textbox in PHP MySQL Using jQuery and Bootstrap with a live demo example.

Sometimes, we need to search for data without loading the whole page. This tutorial shows you how you use an autocomplete search box in PHP MySQL with jQuery and bootstrap form. This tutorial shows you an easy way to autocomplete search with PHP MySQL using jQuery and bootstrap.

Step 1: Database

In this step, we will create a file name db.php and update the below code into your project.

The below code is used to create a MySQL database connection in PHP. we search from MySQL database using PHP, there we will include this file:

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

we need to create an autocomplete search box and update the below code into your autocomplete search box in PHP MySQL.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Autocomplete Search Box in PHP MySQL - Php Coding Stuff</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: PHP Script For Search
<?php
require_once "db.php";
if (isset($_GET['term'])) {
     
   $query = "SELECT * FROM users WHERE name LIKE '{$_GET['term']}%' LIMIT 25";
    $result = mysqli_query($conn, $query);
 
    if (mysqli_num_rows($result) > 0) {
     while ($user = mysqli_fetch_array($result)) {
      $res[] = $user['name'];
     }
    } else {
      $res = array();
    }
    //return json res
    echo json_encode($res);
}
?>
Autocomplete Search Box In Php Mysql - Php Coding Stuff

I hope it can help you...

  1. Usually, I never comment on blogs but your article is so convincing that I never stop myself to say something about it. I am really very happy to say that this post is very interesting to read. You’re doing a great job Man, Keep it up.

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close