Dynamic Dropdown Menu

Ads
 

Dynamic Dropdown Menu

Put records from your database in a drop down menu/list box.

Put records from your database in a drop down menu/list box.

Dynamic Dropdown Menu

     

Put records from your database in a drop down menu/list box. You can apply it as a navigator list box. Good for saving your webpage areas or you have many links you don't want to show all of them in your pages.

First of all you need to create a table in your desired database,  you can create and insert values using phpMyAdmin, for details please visit our web page:

Another way to create table is open the mysql console and type the following:

create table car_list

(c_id int auto_increment, c_name varchar(20), primary key(c_id));

After that insert few values like this:

insert into car_list

values(0, 'aston martin');

and so on.

Note: Whenever we select any field as auto_increment, value starts from 1 and automatically increases by 1 with each record.

 

Example:

<HTML>

<head>

<title>Dynamic Drop Down List</title>

</head>

<BODY>

<form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF; ?>">

Car company :

<select name='NEW'>

<option value="">--- Select ---</option>

<?

mysql_connect("localhost","root","");

mysql_select_db("roseindia");

$select="roseindia";

if(isset($select)&&$select!=""){

$select=$_POST['NEW'];

}

?>

<?

$list=mysql_query("select * from car_list order by c_name asc");

while($row_list=mysql_fetch_assoc($list)){

?>

<option value="<? echo $row_list['c_id']; ?>" <? if($row_list['c_id']==$select){ echo "selected"; } ?>><? echo $row_list['c_name']; ?></option>

<?

}

?>

</select>

<input type="submit" name="Submit" value="Select" />

</form>

</body>

</html>

Output:

Ads

Ads