PHP Bootstrap Autocomplete Tokenfield using Ajax Example

By Hardik Savani May 14, 2024 Category : PHP Bootstrap jQuery MySql Ajax

In this post, i am share with you example of how to make tokenfield autocomplete with ajax in PHP. I will give you very simple example so you can easily use with your core PHP Project and other framework like Laravel, codeigniter etc.

Here in this example we use Bootstrap tokenfield plugin for tags ajax autocomplete. tokenfield plugin is advanced tagging/tokenizing plugin for jQuery and Twitter Bootstrap with a focus on keyboard and copy-paste support. tokenfield plugin also provide typeahead js for tags autocomplete, But it this example i use jquery ui for autocomplete ajax.

In this example i use following files for css and js that way we can make simple example:

1) bootstrap.min.css

2) jquery.min.js

3) jquery-ui.min.css

4) jquery-ui.min.js

5) bootstrap-tokenfield.min.css

6) bootstrap-tokenfield.js

Ok, so let's follow to create just two php files and make example like as bellow preview:


Before create two fies as example we have one "tags" table input your database like as bellow screen shot:

tags table

Create index.php File

<!DOCTYPE html>



<title>PHP - Bootstrap autocomplete tokenfield with Ajax Example</title>

<!-- Css Files-->

<link rel="stylesheet" type="text/css" href="">

<link rel="stylesheet" type="text/css" href="">

<link rel="stylesheet" type="text/css" href="">

<!-- JS Files -->

<script src=""></script>

<script src=""></script>

<script src=""></script>



<div class="container">

<input type="text" class="form-control" id="tokenfield" />


<script type="text/javascript">


autocomplete: {

source: function (request, response) {

jQuery.get("ajaxpro.php", {

query: request.term

}, function (data) {

data = $.parseJSON(data);




delay: 100


showAutocompleteOnFocus: true





Create ajaxpro.php File


define (DB_USER, "root");

define (DB_PASSWORD, "root");

define (DB_DATABASE, "test");

define (DB_HOST, "localhost");

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

$sql = "SELECT name FROM tags

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

LIMIT 10";

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

$json = [];

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

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


echo json_encode($json);


After created successfully both files, you can run by following command:

php -S localhost:8000

Now you can open your browser and run this link : http://localhost:8000

You can get more information about bootstrap tokenfield plugin from here : Tokenfield.

I hope it can help you...