AUTOCOMPLETE FROM DATABASE

Autocomplete from Database

Rate this article

Share this article

Last updated: December 1, 2026. Added a short direct answer, definitions of key terms, clearer headings and a clearer list.

In this tutorial, we will see how to create an autocomplete from a database. We will see how to load data dynamically for an autocomplete suggestion list, Bootstrap Typeahead input field. We will use jQuery AJAX to call the PHP MySQL script to read the data from the database and autocomplete dynamically. The loaded data will be returned to the source of the Typeahead script to list the autocomplete suggestions.

In this example, it stores countries in a database table instead of a Javascript array. The source of the typeahead function is read and supplied from the database by using jQuery AJAX. I used AJAX to execute PHP script by passing the Typeahead field input as the query parameter to process the SELECT to get data for the autocomplete suggestion.

This tutorial shows how to build an autocomplete from a database using Bootstrap Typeahead, jQuery AJAX POST requests, and a PHP MySQL script. The script reads country names from the tbl_country table with a LIKE query and returns them as JSON to populate the suggestion list.

AUTOCOMPLETE FROM DATABASE

Bootstrap Typeahead renders the suggestion list, jQuery AJAX sends the typed value to server.php, PHP runs the LIKE query on MySQL, and JSON carries the matches back.

Steps to build the autocomplete:

  1. Initialize the Typeahead input with jQuery and send the typed value to server.php with an AJAX POST request.
  2. Query the tbl_country table in MySQL with a PHP prepared LIKE statement.
  3. Return the matching country names as JSON to the Typeahead source.

Dynamic Autocomplete from Database

  • Input: Bootstrap Typeahead
  • Request: jQuery AJAX (POST, dataType JSON)
  • Server script: PHP with mysqli prepared statements
  • Database: MySQL database blog_samples, table tbl_country, column country_name

The following jQuery script is used to initialize the Bootstrap Typeahead function to implement autocomplete for an input field. While typing data to the input field, the value is sent to the PHP script as the query parameter using jQuery AJAX POST method. This method received the database response in a JSON format and used as the source for showing autocomplete suggestions.

The JSON data is received and parsed in the AJAX callback by setting the property like dataType: JSON.

 

<script>
    $(document).ready(function () {
        $('#txtCountry').typeahead({
            source: function (query, result) {
                $.ajax({
                    url: "server.php",
					data: 'query=' + query,            
                    dataType: "json",
                    type: "POST",
                    success: function (data) {
						result($.map(data, function (item) {
							return item;
                        }));
                    }
                });
            }
        });
    });
</script>

The following PHP script receives the Typeahead input as the SELECT query parameter. This will be bound to the query statement to get the related country name that starts with the query string. This data will be encoded in JSON format and return to the Typehead source attribute.

Add Autocomplete Search to your website @ 9 USD per month

<?php		
	$keyword = strval($_POST['query']);
	$search_param = "{$keyword}%";
	$conn =new mysqli('localhost', 'root', '' , 'blog_samples');

	$sql = $conn->prepare("SELECT * FROM tbl_country WHERE country_name LIKE ?");
	$sql->bind_param("s",$search_param);			
	$sql->execute();
	$result = $sql->get_result();
	if ($result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
		$countryResult[] = $row["country_name"];
		}
		echo json_encode($countryResult);
	}
	$conn->close();
?>








				
Are you showing the right products, to the right shoppers, at the right time? Contact us to know more.
You may also like