how to select category and subcategory dropdown in php MySQL?

Today, We want to share with you category subcategory dropdown in php mysql.In this post we will show you Category SubCategory Dropdown in PHP MySQL with JQuery AJAX, hear for PHP MySQL Dynamic Category and SubCategory Dependent Dropdown List using Ajax we will give you demo and example for implement.In this post, we will learn about PHP Get Dropdown List Of All Timezones with an example.

PHP mysql Categories and Subcategories Example

Step 1 : Create Table

CREATE TABLE `brands` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) NOT NULL DEFAULT '0',
  `brand` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 2 : Insert some record

INSERT INTO `brands` (`id`, `parent_id`, `brand`) VALUES
(1, 0, 'Bolly4u'),
(2, 0, 'Tamilrokers'),
(3, 1, 'Khatrimaza'),
(4, 1, 'Okhatrimaza'),
(5, 1, 'MoviMaja');

config.php

<?php
    $servername = 'localhost';
    $username   = 'application_v1'; // Username
    $password   = 'Api897898585SD'; // Password
    $dbname     = "brands_shops";
    $conn       = mysqli_connect($servername,$username,$password,"$dbname");
    
    if(!$conn){
        die('Could not Connect MySql Server:' .mysql_error());
    }
?>

Step 4 : Create Category Dropdown Form

index.php

<?php
    include "config.php";
?>
<!DOCTYPE html>
<html>
<head>
    <title>www.pakainfo.com</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <div class="container">
        <div class="row mt-5">
            <div class="col-md-8 offset-2 mt-5">
                <div class="card mt-5">
                    <div class="card-header bg-primary text-white">
                        <h4><b>Brand Subbrand Dropdown in PHP MySQL Ajax - www.pakainfo.com</b></h4>
                    </div>
                    <div class="card-body">
                        <form>
                            <div class="form-group">
                                <label for="PRODUCT-DROPDOWN">Brand</label>
                                <select class="form-control" id="brand-dropdown">
                                    <option value="">Select Brand</option>
                                    <?php
                                        $response = mysqli_query($conn,"SELECT * FROM brands where sub_brand_id = 0");
                                        while($row = mysqli_fetch_array($response)) {
                                    ?>
                                        <option value="<?php echo $row['id'];?>"><?php echo $row["brand"];?></option>
                                    <?php
                                        }
                                    ?>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="SUBPRODUCT">Sub Brand</label>
                                <select class="form-control" id="sub-brand-dropdown">
                                    <option value="">Select Sub Brand</option>
                                </select>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
    $(document).ready(function() {
        $('#brand-dropdown').on('change', function() {
            var brand_id = this.value;
            $.ajax({
                url: "get-subbrand.php",
                type: "POST",
                data: {
                    brand_id: brand_id
                },
                cache: false,
                success: function(response){
                    $("#sub-brand-dropdown").html(response);
                }
            });
        });
    });
    </script>
</body>
</html>

Step 5 : Fetch Sub Brand

get-subbrand.php

<?php
    include "config.php";
    $brand_id = $_POST["brand_id"];
    $response = mysqli_query($conn,"SELECT * FROM brands where sub_brand_id = $brand_id");
?>
<option value="">Select Sub Brand</option>
<?php
    while($row = mysqli_fetch_array($response)) {
?>
<option value="<?php echo $row["id"];?>"><?php echo $row["brand"];?></option>
<?php
    }
?>

I hope you get an idea about Brand SubBrand Dropdown in PHP MySQL with JQuery AJAX.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.