Searchable Select with Select2 Plugin
Searchable Dropdown Demo
Select2 plugin is a jQuery based plugin and replacement for select boxes. It supports searching in the list, finding remote data sets, and pagination (infinite scrolling) of results.
Select2 supports the ability to add options dynamically as the user is typing into the search field.
It also supports multi-value select boxes.
In this tutorial, You are going to learn how to create a basic version of dropdown with a search box using the select2 plugin and read the selected option and print it in PHP.
Step-1
First, create a PHP page withe name index.php with a basic HTML code and add the following links in head section.:
<!-- jQuery --> <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script> <!-- Select2 plugin --> <link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script> <!-- Bootstrap cdn --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
Step-2
Add the Script given in HTML <head></head> tags
<script> $(document).ready(function() { $('.selectpicker').select2(); }); </script>
Step-3
In the body add the given php script.
<?php
if(isset($_POST["submit"])){
$item = $_POST["item"];
echo "Selected Option is ". $item;
}
?>
Step-4
In the HTML <body></body> add the given Form code.
<form method="post" action="index.php"> <select class="selectpicker" data-live-search="true" name="item" data-placeholder="Select an item"> <option data-tokens="donut"> Donut </option> <option data-tokens="muffin">Muffin</option> <option data-tokens="waffle">Waffle</option> <option data-tokens="pancakes">Pancakes </option> </select> <input type="submit" name="submit" value="submit"/> </form>
Step-5
Now the full code looks as show in the below PHP file. Copy this file in XMPP server and run it.
Now the complete page looks as shown in index.php.
Select2demo Full Code in index page
index.php
<!DOCTYPE html> <html> <head> <title>Searchable Select Example Coding-zon</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <head>
<script> $(document).ready(function() { $('.selectpicker').select2(); }); </script>
</head> <body> <div class="container"> <?php if(isset($_POST["submit"])){ $item = $_POST["item"]; echo "Selected Option is ". $item; } ?> <h2>Searchable Dropdown Demo</h2> <p> Using select2 plugin </p>
<?php if(isset($_POST["submit"])){ $item = $_POST["item"]; echo "Selected Option is ". $item; } ?>
<form method="post" action="index.php"> <select class="selectpicker" data-live-search="true" name="item"> <option data-tokens="dpnut"> Donut </option> <option data-tokens="muffin">Muffin</option> <option data-tokens="waffle">Waffle</option> <option data-tokens="pancakes">Pancakes </option> <option data-tokens="sandwich">Sandwich</option> <option data-tokens="burger">Burger</option> <option data-tokens="toast">French Toast</option> </select> <input type="submit" name="submit" value="submit"/>
</form>
</div> </body> <html>
Result:
Comments
Post a Comment