Dropdown List in PHP

10,544 views

Published on

Published in: Education, Technology
4 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total views
10,544
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
192
Comments
4
Likes
1
Embeds 0
No embeds

No notes for slide

Dropdown List in PHP

  1. 1. PHP Tutorials By Vineet Kumar Saini Dropdown List in PHPIntroductionA Dropdown list is a combination of items in a list. In the dropdown list menu only the titleis visible but not the contents. The contents are shown only when the user activates it by asmall arrow. The user may select an item from the menu by dragging the mouse from themenu 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 alist in which the selected item is always visible, and the others are visible on demand byclicking a drop-down button.The main advantages of a drop down list is to save space because the number of views isnot limited by space.There are two types of dropdown lists, which are as follows:  Static dropdown list  Dynamic dropdown list1. Static Dropdown listStatic dropdown list is used in the HTML form. Static means there are no databaseconnectivity with the dropdown list.An example of a static dropdown list is as follows:Code<html><head></head><title>Static Dropdown List</title><body bgcolor="pink">Employee List :<select> <option value="Select">Select</option>} <option value="Vineet">Vineet Saini</option> <option value="Sumit">Sumit Sharma</option> <option value="Dorilal">Dorilal Agarwal</option> <option value="Omveer">Omveer Singh</option> <option value="Rohtash">Rohtash Kumar</option> <option value="Maneesh">Maneesh Tewatia</option> <option value="Priyanka">Priyanka Sachan</option> <option value="Neha">Neha Saini</option></select></body><html>
  2. 2. PHP Tutorials By Vineet Kumar SainiOutput2. Dynamic Dropdown listDynamic dropdown list means, choose items from a list at run time. Dynamic dropdown listis used in the PHP form. Dynamic means there is database connectivity available forthe dropdown list.Step by step solution to create dynamic dropdown listStep 1. First of all we create a database in MySql using the following query. CREATE DATABASE `company`;Step 2. After that then we create a table in the database. For create table in the databaseyou can use the following query. CREATE TABLE `company`.`employee` (`emp_id` INT(10) NOT NULLAUTO_INCREMENT, `emp_name` VARCHAR(30) NOT NULL, PRIMARY KEY(`emp_id`)) ENGINE = InnoDB;Step 3. Now we insert the values in the table using the following queries. insert into employee (emp_id,emp_name) values(0,Vineet Saini); insert into employee (emp_id,emp_name) values(0,Sumit Sharma); insert into employee (emp_id,emp_name) values(0,Dorilal Agarwal); insert into employee (emp_id,emp_name) values(0,Omveer Singh); insert into employee (emp_id,emp_name) values(0,Maneesh Tewatia);
  3. 3. PHP Tutorials By Vineet Kumar Saini insert into employee (emp_id,emp_name) values(0,Rohtash Kumar); insert into employee (emp_id,emp_name) values(0,Priyanka Sachan); 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.Step 5. Now we will use the above table in the dropdown list using the following code.Code<html> <head> <title>Dynamic Drop Down List</title> </head> <BODY bgcolor ="pink"> <form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF;?>"> Employee List : <select Emp Name=NEW> <option value="">--- Select ---</option> <? mysql_connect ("localhost","root",""); mysql_select_db ("company"); $select="company"; if (isset ($select)&&$select!=""){ $select=$_POST [NEW]; } ?> <? $list=mysql_query("select * from employee order by emp_id asc"); while($row_list=mysql_fetch_assoc($list)){ ?> <option value="<? echo $row_list[emp_id]; ?>"<?if($row_list[emp_id]==$select){ echo "selected"; } ?>> <?echo $row_list[emp_name];?>
  4. 4. PHP Tutorials By Vineet Kumar Saini </option> <? } ?> </select> <input type="submit" name="Submit" value="Select" /> </form> </body></html>OutputConclusionSo 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 listsin PHP.

×