PHP MySQL Select2 Dropdown Example


Share:
 | Published By: Admin | Published On: Feb 28, 2022 | Category: PHP


Hello Friends,

I am going to explain you php mysql select2 example. You will learn php mysql select query example. In side this article we will see php example select from MySQL. Select2 is a widely used jQuery plugin. It makes select elements user-friendly.

This article will give you simple example of how to php mysql select2 example drop down list with core php mysql. We will use get search how to php mysql select2 example drop down list.

You can use from how to use select2 in php. I will give you simple mysql select examples query.

So, let's see bellow solution:

index.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP MySQL Select2 Example</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha256-aAr2Zpq8MZ+YA/D6JtRD3xtrwpEz2IqOS+pWD/7XKIw=" crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha256-OFRAJNoaD8L3Br5lglV7VyLRf0itmoBzWUoM+Sji4/8=" crossorigin="anonymous"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
<div class="row mt-5">

    <?php 

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "db_php";

        $conn = new mysqli($servername, $username, $password, $dbname);

        $sql = "SELECT * FROM `post`";

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

    ?>

        <div class="col-md-6 offset-3 mt-5">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <h4>PHP MySQL Select2 Example - Mywebtuts.com</h4>
                </div>
                <div class="card-body" style="height: 280px;">
                    <div>

                        <select class="select2 form-control" data-rel="chosen"  name="degree_id" id="selectError">
                            <?php 
                                while ($row = $result->fetch_assoc()) {
                                    echo "<option>".$row["title"]."</option>";
                                }
                            ?>
                        </select>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        $('.select2').select2({});
    </script>
    
</body>
</html>
Output:

It will help you...
Share: