Dynamic Dependent drop down list using html,php,mysql
Posted Date:08-05-2017
In this post we will explain how to dropdown list dynamically using html,php
First we explain dynamic dropdown list using php
Dynamic dropdown list using php
Step 1: Create a new file dropdown.php
step 2: Create a mysql connection code inside dropdown.php
$mysqlserver="localhost"; $mysqlusername="root"; $mysqlpassword=""; $link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error()); $dbname = 'test'; mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
stp3 : Create mysql table ls_countries
CREATE TABLE `ls_countries` ( `country_id` int(11) NOT NULL AUTO_INCREMENT, `sortname` varchar(3) NOT NULL, `name` varchar(150) NOT NULL, `phonecode` int(11) NOT NULL, `status` int(11) NOT NULL COMMENT '1->active,0->deactive', PRIMARY KEY (`country_id`)) ENGINE=InnoDB AUTO_INCREMENT=247 DEFAULT CHARSET=utf8
step4 : Create drop down list dynamically using following code
<select> <option>Select Country</option> <?php $cdquery="SELECT * FROM ls_countries"; $cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error()); while ($cdrow=mysql_fetch_array($cdresult)) { $cdTitle=$cdrow["name"]; echo "<option>$cdTitle </option>"; } ?> </select>
we are following above steps and getting code like this
<?php $mysqlserver="localhost"; $mysqlusername="root"; $mysqlpassword=""; $link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error()); $dbname = 'linus'; mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error()); ?> <!DOCTYPE html> <html> <head> <title>Drop Down Using Php</title> </head> <body> <select> <option>Select Country</option> <?php $cdquery="SELECT * FROM ls_countries"; $cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error()); while ($cdrow=mysql_fetch_array($cdresult)) { $cdTitle=$cdrow["name"]; echo "<option>$cdTitle </option>"; } ?> </select> </body> </html>