SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
PHP Tutorials By Vineet Kumar Saini

                            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 advantages 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 are no database
connectivity 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>
PHP Tutorials By Vineet Kumar Saini

Output




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 dynamic dropdown list

Step 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 database
you can use the following query.

      CREATE TABLE `company`.`employee` (`emp_id` INT(10) NOT NULL
AUTO_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');
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'];?>
PHP Tutorials By Vineet Kumar Saini

             </option>
          <?
          }
          ?>
       </select>
       <input type="submit" name="Submit" value="Select" />
     </form>
  </body>
</html>

Output




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.

Contenu connexe

Tendances

Mysql Aggregate
Mysql AggregateMysql Aggregate
Mysql Aggregate
lotlot
 
Ôn tập KTTMDT
Ôn tập KTTMDTÔn tập KTTMDT
Ôn tập KTTMDT
mrcoffee282
 
The go-start webframework (GTUG Vienna 27.03.2012)
The go-start webframework (GTUG Vienna 27.03.2012)The go-start webframework (GTUG Vienna 27.03.2012)
The go-start webframework (GTUG Vienna 27.03.2012)
ungerik
 

Tendances (20)

Java script functions
Java script   functionsJava script   functions
Java script functions
 
Elm intro
Elm introElm intro
Elm intro
 
Jsf lab
Jsf labJsf lab
Jsf lab
 
Elm: delightful web development
Elm: delightful web developmentElm: delightful web development
Elm: delightful web development
 
HTML Form Part 1
HTML Form Part 1HTML Form Part 1
HTML Form Part 1
 
HTML5 Form Validation
HTML5 Form ValidationHTML5 Form Validation
HTML5 Form Validation
 
Mysql Aggregate
Mysql AggregateMysql Aggregate
Mysql Aggregate
 
Print function in PHP
Print function in PHPPrint function in PHP
Print function in PHP
 
Php variables (english)
Php variables (english)Php variables (english)
Php variables (english)
 
Ôn tập KTTMDT
Ôn tập KTTMDTÔn tập KTTMDT
Ôn tập KTTMDT
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in php
 
HTML5 workshop, forms
HTML5 workshop, formsHTML5 workshop, forms
HTML5 workshop, forms
 
Victoria wordpress
Victoria wordpressVictoria wordpress
Victoria wordpress
 
Technical training sample
Technical training sampleTechnical training sample
Technical training sample
 
$.Template
$.Template$.Template
$.Template
 
Php workshop L03 superglobals
Php workshop L03 superglobalsPhp workshop L03 superglobals
Php workshop L03 superglobals
 
Php workshop L04 database
Php workshop L04 databasePhp workshop L04 database
Php workshop L04 database
 
The go-start webframework (GTUG Vienna 27.03.2012)
The go-start webframework (GTUG Vienna 27.03.2012)The go-start webframework (GTUG Vienna 27.03.2012)
The go-start webframework (GTUG Vienna 27.03.2012)
 
Ruby - Design patterns tdc2011
Ruby - Design patterns tdc2011Ruby - Design patterns tdc2011
Ruby - Design patterns tdc2011
 
Front-End Methodologies
Front-End MethodologiesFront-End Methodologies
Front-End Methodologies
 

En vedette (8)

Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
PHP Technical Questions
PHP Technical QuestionsPHP Technical Questions
PHP Technical Questions
 
Chapter 1 computer hardware and flow of information
Chapter 1 computer hardware and flow of informationChapter 1 computer hardware and flow of information
Chapter 1 computer hardware and flow of information
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Similaire à Dropdown List in PHP

Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
guest5d87aa6
 
Creating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemCreating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login System
Azharul Haque Shohan
 
Your Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckYour Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages Suck
Anthony Montalbano
 

Similaire à Dropdown List in PHP (20)

Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
Creating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemCreating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login System
 
PHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web DevelopmentPHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web Development
 
Java Script
Java ScriptJava Script
Java Script
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
PHP and MySQL.ppt
PHP and MySQL.pptPHP and MySQL.ppt
PHP and MySQL.ppt
 
Your Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckYour Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages Suck
 
Clever Joomla! Templating Tips and Tricks
Clever Joomla! Templating Tips and TricksClever Joomla! Templating Tips and Tricks
Clever Joomla! Templating Tips and Tricks
 
Geek Moot '09 -- Smarty 101
Geek Moot '09 -- Smarty 101Geek Moot '09 -- Smarty 101
Geek Moot '09 -- Smarty 101
 
HTML and CSS part 2
HTML and CSS part 2HTML and CSS part 2
HTML and CSS part 2
 
lect4
lect4lect4
lect4
 
lect4
lect4lect4
lect4
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
Php
PhpPhp
Php
 
PHP and Databases
PHP and DatabasesPHP and Databases
PHP and Databases
 
Slimme Joomla! Templating Tips en Truuks
Slimme Joomla! Templating Tips en TruuksSlimme Joomla! Templating Tips en Truuks
Slimme Joomla! Templating Tips en Truuks
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
Difference between mysql_fetch_array and mysql_fetch_assoc in PHPDifference between mysql_fetch_array and mysql_fetch_assoc in PHP
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
 

Plus de Vineet Kumar Saini

Plus de Vineet Kumar Saini (20)

Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Browser information in PHP
Browser information in PHPBrowser information in PHP
Browser information in PHP
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Variables in PHP
Variables in PHPVariables in PHP
Variables in PHP
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
 
SQL Limit in PHP
SQL Limit in PHPSQL Limit in PHP
SQL Limit in PHP
 
CSS in HTML
CSS in HTMLCSS in HTML
CSS in HTML
 
Top 100 .Net Interview Questions and Answer
Top 100 .Net Interview Questions and AnswerTop 100 .Net Interview Questions and Answer
Top 100 .Net Interview Questions and Answer
 

Dernier

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

Dropdown List in PHP

  • 1. PHP Tutorials By Vineet Kumar Saini 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 advantages 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 are no database connectivity 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. PHP Tutorials By Vineet Kumar Saini Output 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 dynamic dropdown list Step 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 database you can use the following query. CREATE TABLE `company`.`employee` (`emp_id` INT(10) NOT NULL AUTO_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. 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. PHP Tutorials By Vineet Kumar Saini </option> <? } ?> </select> <input type="submit" name="Submit" value="Select" /> </form> </body> </html> Output 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.