Dropdown List in PHP

Introduction

 
A Dropdown list is a combination of items in a list. In the dropdown list menu, only the title is visible but not the contents. The contents are shown only when the user activates it by a small arrow. The user may select an item from the menu by dragging the mouse from the menu title or by clicking the title and then clicking the item. The dropdown list box is read-only.
 
We can say a drop-down list is a box containing a list of multiple items. A drop-down list is a list in which the selected item is always visible, and the others are visible on demand by clicking a drop-down button.
 
The main advantage of a drop-down list is to save space because the number of views is not limited by space.
 
There are two types of dropdown lists, which are as follows:
  • Static dropdown list
  • Dynamic dropdown list
1. Static Dropdown list
 
Static dropdown list is used in the HTML form. Static means there is no database connectivity with the dropdown list.
 
An example of a static dropdown list is as follows:
 
Code
  1. <html>  
  2. <head></head>  
  3. <title>Static Dropdown List</title>  
  4. <body bgcolor="pink">  
  5. Employee List :  
  6. <select>  
  7.   <option value="Select">Select</option>}  
  8.   <option value="Vineet">Vineet Saini</option>  
  9.   <option value="Sumit">Sumit Sharma</option>  
  10.   <option value="Dorilal">Dorilal Agarwal</option>  
  11.   <option value="Omveer">Omveer Singh</option>  
  12.   <option value="Rohtash">Rohtash Kumar</option>  
  13.   <option value="Maneesh">Maneesh Tewatia</option>  
  14.   <option value="Priyanka">Priyanka Sachan</option>  
  15.   <option value="Neha">Neha Saini</option>  
  16. </select>   
  17. </body>  
  18. <html>  
Output
 
image3.jpg
 
2. Dynamic Dropdown list
 
Dynamic dropdown list means, choose items from a list at run time. Dynamic dropdown list is used in the PHP form. Dynamic means there is database connectivity available for the dropdown list.
 
Step by step solution to create a dynamic dropdown list
 
Step 1. First of all, we create a database in MySql using the following query.
  1. CREATE DATABASE `company`;  
Step 2. After that then we create a table in the database. For create table in the database you can use the following query.
  1. CREATE TABLE `company`.`employee` (`emp_id` INT(10) NOT NULL AUTO_INCREMENT, `emp_name` VARCHAR(30) NOT NULLPRIMARY KEY(`emp_id`)) ENGINE = InnoDB;  
Step 3. Now we insert the values in the table using the following queries.
  1. insert into employee (emp_id,emp_name) values(0,'Vineet Saini');  
  2. insert into employee (emp_id,emp_name) values(0,'Sumit Sharma');  
  3. insert into employee (emp_id,emp_name) values(0,'Dorilal Agarwal');  
  4. insert into employee (emp_id,emp_name) values(0,'Omveer Singh');  
  5. insert into employee (emp_id,emp_name) values(0,'Maneesh Tewatia');  
  6. insert into employee (emp_id,emp_name) values(0,'Rohtash Kumar');  
  7. insert into employee (emp_id,emp_name) values(0,'Priyanka Sachan');  
  8. insert into employee (emp_id,emp_name) values(0,'Neha Saini');  
Step 4. When we execute the above query then you will get the following table.
 
image1.jpg
 
Step 5. Now we will use the above table in the dropdown list using the following code.
 
Code
  1. <html>  
  2.     <head>  
  3.     <title>Dynamic Drop Down List</title>  
  4.     </head>  
  5.     <BODY bgcolor ="pink">  
  6.         <form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF; ?>">  
  7.             Employee List :  
  8.             <select Emp Name='NEW'>  
  9.             <option value="">--- Select ---</option>  
  10.             <?  
  11.                 mysql_connect ("localhost","root","");  
  12.                 mysql_select_db ("company");  
  13.                 $select="company";  
  14.                 if (isset ($select)&&$select!=""){  
  15.                 $select=$_POST ['NEW'];  
  16.             }  
  17.             ?>  
  18.             <?  
  19.                 $list=mysql_query("select * from employee order by emp_id asc");  
  20.             while($row_list=mysql_fetch_assoc($list)){  
  21.                 ?>  
  22.                     <option value="<? echo $row_list['emp_id']; ?>"<? if($row_list['emp_id']==$select){ echo "selected"; } ?>>  
  23.                                          <?echo $row_list['emp_name'];?>  
  24.                     </option>  
  25.                 <?  
  26.                 }  
  27.                 ?>  
  28.             </select>  
  29.             <input type="submit" name="Submit" value="Select" />  
  30.         </form>  
  31.     </body>  
  32. </html>  
Output
 
image2.jpg
 

Conclusion

 
So in this article, you saw how to create a static dropdown list and dynamic dropdown list. Using this article one can easily understand how to create static and dynamic dropdown lists in PHP.
 
Some Helpful Resources


Similar Articles