PHP Bootstrap - dynamic autocomplete tag input using jquery plugin

PHP Bootstrap - dynamic autocomplete tag input using jquery plugin

In this tutorial, I will tell you how to implement multiple input tags with dynamic data from MySQL database table using jquery plugin "tagmanager" and "typeahead" JS.

This is required in the application in which you need to show user selected options in a text area in tags with remove option.

This functionality can be done very easily by using jQuery "tagmanager" plugin for multiple input tags based on user selection from dynamic list of data.

Using Tag Manager plugin you can manage input tags separately from each input with better layout.

There are so many plugin available for multiple input tags but Tag Manager plugin can be easily implemented into application with different tag styles.

You can trigger the events to get notification when things happen. For example, when you are going to push the new tags or you have pushed a new tag and also when you remove any tag then you can hook on these events.

To use tag manager you need to have following libraries :

  • bootstrap.min.css
  • tagmanager.min.css
  • jquery.min.js
  • bootstrap.min.js
  • tagmanager.min.js
  • bootstrap3-typeahead.min.js

For this example, I have a database table with all countries :

country-table

Now create a "index.php" file for testing :

index.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>PHP Bootstrap - dynamic autocomplete tag input using jquery tag manager plugin </title>
  5.     <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  6.     <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/tagmanager/3.0.2/tagmanager.min.css">
  7.     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  8.     <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  9.     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tagmanager/3.0.2/tagmanager.min.js"></script>
  10.     <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>
  11. </head>
  12. <body>
  13. <div class="container">
  14.     
  15.         <div class="form-group">
  16.             <label>Add Country Tags:</label>
  17.             <input type="text" name="countries" placeholder="Type here.." class="typeahead tm-input form-control tm-input-info"/>
  18.         </div>
  19. </div>
  20. <script type="text/javascript">
  21. $(document).ready(function() {
  22. var tags = $(".tm-input").tagsManager();
  23. jQuery(".typeahead").typeahead({
  24. source: function (query, process) {
  25. return $.get('aj.php', { query: query }, function (data) {
  26. data = $.parseJSON(data);
  27. return process(data);
  28. });
  29. },
  30. afterSelect :function (item){
  31. tags.tagsManager("pushTag", item);
  32. }
  33. });
  34. });
  35. </script>
  36. </body>
  37. </html>
country.php

  1. <?php
  2.     
  3.     $mysqli = new mysqli("host", "username", "password", "database");
  4.     $sql = "SELECT name FROM countries
  5.             WHERE name LIKE '%".$_GET['query']."%'
  6.             LIMIT 10";
  7.     $result = $mysqli->query($sql);
  8.     $data = [];
  9.     while($row = $result->fetch_assoc()){
  10.      $data[] = $row['name'];
  11.     }
  12.     echo json_encode($data);
  13. ?>

Add Multiple Email addresses as tags in single text input Example

Phone: (+91) 8800417876
Noida, 201301