ItSolutionStuff.com

Bootstrap Dynamic Autocomplete search using Typeahead JS

By Hardik Savani • November 5, 2023
PHP Bootstrap HTML jQuery Typeahead JS

In this post i am going to show you how to add dynamically Autocomplete using typeahead dynamic. In this post i give you full example that way you can run this example easily in your project. after run this example you can see output like bellow preview.

First we will create ajax.html file for html layout in this file i added jquery and Bootstrap Typeahead code. When you write on input box then you can find Dynamic Autocomplete search using ajax requiest. So, first create aja.html file and copy bellow code.

ajax.html

<html lang="en">

<head>

<title>Bootstrap Typeahead with Ajax Example</title>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>

</head>

<body>


<div class="row">

<div class="col-md-12 text-center">

<br/>

<h1>Search Dynamic Autocomplete using Bootstrap Typeahead JS</h1>

<input class="typeahead form-control" style="margin:0px auto;width:300px;" type="text">

</div>

</div>


<script type="text/javascript">


$('input.typeahead').typeahead({

source: function (query, process) {

return $.get('/ajaxpro.php', { query: query }, function (data) {

console.log(data);

data = $.parseJSON(data);

return process(data);

});

}

});


</script>

</body>

</html>

Ok, now we need to create another page for getting json data by ajax requiest. So create another page ajaxpro.php file and copy bellow code.

ajaxpro.php

<?php


define (DB_USER, "root");

define (DB_PASSWORD, "root");

define (DB_DATABASE, "learn");

define (DB_HOST, "localhost");

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);


$sql = "SELECT items.title FROM items

WHERE title LIKE '%".$_GET['query']."%'

LIMIT 10";

$result = $mysqli->query($sql);


$json = [];

while($row = $result->fetch_assoc()){

$json[] = $row['title'];

}


echo json_encode($json);

Try this.....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Install Bootstrap 5 in Angular 13?

Read Now →

React Bootstrap Popovers Example

Read Now →

Angular 9/8 Bootstrap 4 Datepicker Example

Read Now →

How to use Bootstrap Modal in Angular?

Read Now →

Bootstrap Datepicker Trigger Change Event Example

Read Now →

Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo

Read Now →

Bootstrap Datepicker Change Date Format Example

Read Now →

Bootstrap Colorpicker Example Code

Read Now →

Bootstrap Timepicker using Datetimepicker JS Example

Read Now →

JQuery Delete Confirm Modal using Bootbox Example

Read Now →

How to Add Tooltip in Bootstrap?

Read Now →

Multi Select Autocomplete JQuery Example with Code

Read Now →

Autocomplete with Images and Custom HTML Code in Jquery UI?

Read Now →