Dropdown search box jquery example with select2.min.js

Dropdown search box jquery example with select2.min.js

Hi friends, In this tutorial, we will discuss regarding dropdown search box jquery example with select2.min.js plugin. The default dropdown box in HTML or bootstrap does not have the option for searching data. Sometimes, we need to search for individual data from huge data sets in select boxes.

In such a case, select2.min.js plugin helps us to do so and gives us the search box option. With the help of this plugin, we can easily search for data from the select options as per our needs.

There are two ways available to use this plugin successfully

  1. Use the plugin from the CDN network directly.
  2. Download the plugin from Github and install it to use in your source code or HTML file.

How to use select2.min.js from a CDN (Content delivery network)

This plugin is available in the jsDelivr and cdnjs. These are the CDN network for jQuery. You can simply include the below lines inside the head section of your web page or HTML file.

<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>

How to install select2.min.js in localhost or locally for the dropdown search box

  • To install this plugin, you can simply download from the above-mentioned link from Github and extract the plugin zip file and place the extracted folder where you have created your PHP file in the root directory.
  • You can find the CSS and js files for select2.min.js plugin inside the dist folder of your extracted folder.
    Include the CSS file in the head section of the web page as shown below
<link rel="stylesheet" href="select2/dist/css/select2.min.css" crossorigin="anonymous">
  • Include the js file in the script tag before the end of the body tag as shown below
  <script src="select2/dist/js/select2.min.js"></script>
  • Include the below code inside the script tag
<script>
   $(document).ready(function(){
     $('#city').select2();
   });
</script>

Browser Compatibility of select2.min.js

It supports in the following browsers

  • IE 8+
  • Chrome 8+
  • Firefox 10+
  • Safari 3+
  • Opera 10.6+

In this tutorial, I am using a table name “state” from my database to put the data sets inside the select box options.

DDL information of the table

CREATE TABLE state (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
state_name varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Use a PHP script to fetch the table data from database as shown below

<?php
include('connect.php');

	$stmt = $con->prepare("select * from state");
	$stmt->execute();
	$state_details = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

Note:-

Download the bootstrap CSS and js files from google and include the path of the files in the href attribute of link tag and src attribute of the script tag respectively as shown below.

<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>

Complete Code:-

<?php
include('connect.php');

	$stmt = $con->prepare("select * from state");
	$stmt->execute();
	$state_details = $stmt->fetchAll(PDO::FETCH_ASSOC);
	
?>
<html>
<head>
<title>ajax example</title>
<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">
<link rel="stylesheet" href="select2/dist/css/select2.min.css" crossorigin="anonymous">
<style>
.container{
	width:50%;
	height:30%;
	padding:20px;
}
</style>
</head>

<body>
	<div class="container">
	<h3><u>Search data in select boxes with select2.min.js</u></h3>
	<br/><br/>
	<div class="row">
		<div class="col-sm-8">
			<div class="form-group">
			    <label for="name">City Names:</label>
			    <select class="form-control form-control-sm" name="city_name" id="city">
			    	<option value="">Please Select</option>
		<?php
		foreach ($state_details as $key => $value) {
			# code...
		     echo '<option value="'.$value['id'].'">'.$value['state_name'].'</option>';
			
		}
		?>
	</select>
</div>
</div>
</div>

<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="select2/dist/js/select2.min.js"></script>
<script>
   $(document).ready(function(){
     $('#city').select2();
   });
</script>
</body>
</html>

Also, Read How to export MySQL table data into excel using jQuery

Conclusion:

I hope this article will help you to understand the working of select2.min.js plugin. If you have any doubt then please leave a comment below.

1 thought on “Dropdown search box jquery example with select2.min.js”

  1. Hi there, I believe your blog could be having web browser compatibility issues.
    When I take a look at your web site in Safari, it looks fine however when opening in IE,
    it has some overlapping issues. I just wanted to give you a quick
    heads up! Other than that, wonderful site!

    Reply

Leave a Comment

THOR311

THOR311

THOR311

HOKI311

thor311

thor311

thor311

thor311

thor311

THOR311

THOR311

THOR311

thor311

thor311

KUPU178

thor311

thor311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

HOKI311

sbobet WAP

hoki311

sabung ayam online

hoki311

hoki311

live casino online

HOKI311

hoki311

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

mix parlay sbobet

hoki311

hoki311

hoki 311

togel hoki311

hoki311

THOR311

THOR311

THOR311

ceri188

ceri188

ceri188

ratu311 slot

kupu178 toto hongkong

hoki311

hoki311

SABA Sport

sbobet

hoki311

THOR311

RATU311

kupu178 togel online

kupu178 slot demo

slot ratu311

pola slot gacor

ratu311

live casino online

hoki311

slot gacor online

hoki311

mix parlay

hoki311

sabung ayam online

hoki311

THOR311

SBOBET WAP

Judi Bola Online

hoki311

hoki311

slot gacor

sabung ayam online

hoki311

hoki311

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

ceri188

ceri188

ceri188

togel online

hoki311

hoki311

sabung ayam online

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot mahjong

ceri188

THOR311

THOR311

thor311 aplikasi

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor dingdong

ceri188

ceri188

kupu178

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311