ItSolutionStuff.com

PHP Bootstrap Autocomplete Tokenfield using Ajax Example

By Hardik Savani • May 14, 2024
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:

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>

<html>

<head>


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


<!-- Css Files-->

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

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tokenfield/0.12.0/css/bootstrap-tokenfield.min.css">


<!-- JS Files -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tokenfield/0.12.0/bootstrap-tokenfield.js"></script>


</head>

<body>


<div class="container">

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

</div>


<script type="text/javascript">

$('#tokenfield').tokenfield({

autocomplete: {

source: function (request, response) {

jQuery.get("ajaxpro.php", {

query: request.term

}, function (data) {

data = $.parseJSON(data);

response(data);

});

},

delay: 100

},

showAutocompleteOnFocus: true

});

</script>


</body>

</html>

Create ajaxpro.php File

<?php


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...

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

PHP MySQL Login with Google Account Example

Read Now →

PHP MySQL Contact US Form with Validation Example

Read Now →

PHP Ajax Inline Editing using X-editable Bootstrap JS Example

Read Now →

PHP Ajax Dependent Dropdown List Example

Read Now →

Tags Input with Autocomplete using jQuery and PHP Example

Read Now →

Laravel Dynamic Autocomplete Search using Select2 JS Ajax - Part 1

Read Now →

Multiple File Upload using Dropzone JS in PHP Example

Read Now →

PHP Import Excel File into MySQL Database Tutorial

Read Now →

PHP CRUD Operation Using Ajax and JQuery Example

Read Now →

PHP Autocomplete Websites with Domain using Clearbit API

Read Now →

PHP Remove Duplicates from Multidimensional Array Example

Read Now →

PHP Crop Image Before Upload using Croppie JS Example

Read Now →

PHP JQuery Select2 Ajax Autocomplete Example

Read Now →

Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo

Read Now →

PHP Paypal Payment Gateway Integration Example

Read Now →