Creating Dynamic Drop Down List in PHP

Introduction

In this article I will explain creation of a Dynamic Drop Down List in PHP using a MySQL database. Such that, when you want to insert, delete or update data in our drop down list box, you directly insert, update or delete from the database. This phenomenon is called a dynamic drop down list. This drop down list dynamically fetches data from the database. This phenomenon I will describe here just with an example. 
 
Dynamic Dropdown List.jpg

Example


In the following code, some car names are in my database and these car names are shown in our dropdown list dynamically.

 

<HTML>

<head>

<title>Dynamic Drop Down List</title>

</head>

<Body background="as.jpg">

<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">

Car Name:

<select name='NEW'>

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

<?php

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

mysql_select_db("vinod");

$select="vinod";

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

$select=$_POST['NEW'];

}

?>

<?php

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

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

?>

<option value="<?php echo $row_list['c_id']; ?>">

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

 <?php echo $row_list['c_name']; ?>

</option>

<?php

}

?>

</select>

</form>

</body>

</html>


dropdown list with database.jpg

dropdown list with database1.jpg

I will just insert some car names into our table and then refresh our localhost browser so the dropdown list box is automatically updated dynamically. Such that:

dropdown list with database2.jpg

dropdown list with database3.jpg


Similar Articles