Autocomplete Textbox using jQuery AJAX in PHP MySql

In this post you will learn How to autocomplete textbox using jQuery AJAX in PHP MySQL.The Ajax Autocomplete text box is very user-friendly for any website.

The Autocomplete text box allows users to quickly find and select values ​​from a list of pre-populated options. Suggestions are displayed automatically when the user enters the field.

By using jQuery with Ajax we can easily present automated proposals for all queries from the database in the text field. I don’t use the jQuery plugin to ask for text fields for autocomplete, but I do use the simple Ajax method with PHP and MySQL for autocomplete text fields. Let’s start with a tutorial on how to fill in text boxes automatically.

Here we present text fields for entering a city name. When users enter the city name, they display the city name starting with the characters they entered in the text box and the city displayed below the text box. This type of city is automatically selected from the users table from the MySQL database. 

Create MySql Connection in PHP

For importing and exporting database in MySql will make a separate file ‘dbCongif.php’. Add the following code and replace the database credentials with yours. You can find your db credentials in Application Access details:

We first need to create a database connection file called dbConfig.php to extract the city name from the database.

	// Database configuration 
	$dbHost     = "localhost"; 
	$dbUsername = "root"; 
	$dbPassword = ""; 
	$dbName     = "testing"; 
	// Create database connection 
	$con = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); 
	// Check connection 
	if ($con->connect_error) { 
	    die("Connection failed: " . $con->connect_error); 

Create an autocomplete search form

Now create the HTML search form page for searching the city name and autocomplete 


<!Doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>Autocomplete Textbox using jQuery AJAX in PHP MySql</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <script src=""></script>
<style type="text/css">
      margin-top: 0px;
      background: #fff;
      color: #000;
      padding: 12px;
      cursor: pointer;
      color: black;
      background: #f0f0f0;
  <body style="background-color: #ebebeb">
    <div class="container" style="margin-top: 50px;">
      <h2 class="text-center">Autocomplete Textbox using jQuery AJAX in PHP MySql</h2>
      <div class="row">
        <div class="col-md-3"></div>  
        <div class="col-md-6" style="margin-top:20px; margin-bottom:20px;">
            <div class="form-group">
              <input type="text" class="form-control" name="cityname" id="city" placeholder="Search City"> 
              <div id="citylist"></div>

<!--- Autocomplete textbox jquery ajax --->
<script type="text/javascript">
      $("#city").on("keyup", function(){
        var city = $(this).val();
        if (city !=="") {

      // click one particular city name it's fill in textbox
      $(document).on("click","li", function(){

PHP Code for search data from database


	// include database connection file

	include_once "dbConfig.php";

	// autocomplete textbox jquery ajax in PHP
	if (isset($_POST['city'])) {

  		$output = "";
  		$city = $_POST['city'];
  		$query = "SELECT * FROM users WHERE city_name LIKE '%$city%'";
  		$result = $con->query($query);

  		$output = '<ul class="list-unstyled">';		

  		if ($result->num_rows > 0) {
  			while ($row = $result->fetch_array()) {
  				$output .= '<li>'.ucwords($row['city_name']).'</li>';
  			  $output .= '<li> City not Found</li>';
	  	$output .= '</ul>';
	  	echo $output;



Autocomplete Textbox using jQuery AJAX in PHP MySql

You can always support by sharing on social media or recommending my blog to your friends and colleagues. If you have any suggestions or problems about this tutorial, please comment on the  form below.😊

Autocomplete Textbox using jQuery AJAX in PHP MySql

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *