SlideShare una empresa de Scribd logo
1 de 4
Descargar para leer sin conexión
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.

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado (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
 

Similar a 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
 

Similar a 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
 

Más de Vineet Kumar Saini

Más 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
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

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
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
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...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

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.