two dependent dropdown in php – Dependent Drop-down List in PHP using jQuery AJAX with Category SubCategory Example
Table of Contents
Two Dependent Dropdown In PHP
1.itsolutionstuck_category
2.itsolutionstuck_subcategory
Step 1: Create Tables and Dummy Data
itsolutionstuck_category table:
[php]
CREATE TABLE `itsolutionstuck_category` (
`id` int(11) NOT NULL,
`name` varchar(155) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[/php]
itsolutionstuck_subcategory table:
[php]
CREATE TABLE `itsolutionstuck_subcategory` (
`id` int(11) NOT NULL,
`category_id` int(12) NOT NULL,
`name` varchar(155) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[/php]
Step 2: Create index.php file
index.php
[php]
[/php]
Step 3: Add DB Configuration File
db_config.php
[php]
[/php]
Step 4: Add Ajax File
ajaxpro.php
[php]
query($sql);
$json = [];
while($row = $result->fetch_assoc()){
$json[$row[‘id’]] = $row[‘name’];
}
echo json_encode($json);
?>
[/php]