Jquery autocomplete example with ajax | Sanwebcorner

Jquery autocomplete example with ajax

In this post i will tell you how to create Jquery ajax autocomplete textbox with mysql database table. This example is really helpful and it is very user friendly because instead of type the full text will give the perfect suggestion to the user. So the user easy to select the particular text from the suggestion. This auto complete text will shows when the user enters one or more text in the text field. Here i used the ajax concept so without page loading the data will be shows instantly. It may used for search box or any other text boxes in the form fields.

The below example have the live demo and the source code. Use this code according to your project requirments. Autocomplete is very useful and important concept, It giving the user a list of matching results as they type can both save the users time and offer them an exact list of results. So this is one of the simple and important concept. I hope this post is very useful.


<TITLE>jQuery AJAX Autocomplete with example</TITLE>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
type: "POST",
url: "readdata.php",
beforeSend: function(){
$("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
success: function(data){

function selectCountry(val) {
<div class="frmSearch">
<input type="text" id="search-box" placeholder="Country Name" />
<div id="suggesstion-box"></div>


class DBController {
private $host = "localhost";
private $user = "username";
private $password = "password";
private $database = "database_name";
private $conn;

function __construct() {
$this->conn = $this->connectDB();

function connectDB() {
$conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
return $conn;

function runQuery($query) {
$result = mysqli_query($this->conn,$query);
while($row=mysqli_fetch_assoc($result)) {
$resultset[] = $row;
return $resultset;

function numRows($query) {
$result  = mysqli_query($this->conn,$query);
$rowcount = mysqli_num_rows($result);
return $rowcount;


$db_handle = new DBController();
if(!empty($_POST["keyword"])) {
$query ="SELECT * FROM country WHERE country_name like '" . $_POST["keyword"] . "%' ORDER BY country_name LIMIT 0,6";
$result = $db_handle->runQuery($query);
if(!empty($result)) {
<ul id="country-list">
foreach($result as $country) {
<li onClick="selectCountry('<?php echo $country["country_name"]; ?>');"><?php echo $country["country_name"]; ?></li>
<?php } ?>
<?php } } ?>

Post a comment


360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap Bootstrap form validation border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll create random number Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed donation form download zip email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient heart animation hide menu Horizontal News ticker Hover Htm Html html dropdown html form html form validation html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript javascript validation Jquery jquery validation js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form multi-select-checkbox Multi-Step-form Multiple File Upload in PHP mysql News ticker on hover change text to image our team page password generator password strength checker paypal form paypal html form Photoshop PHP php mailer populate html selectbox Popup image on click the link pure css Question type form random number generate Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips whatsapp link website Whislist Youtube