How to Retrieve Data from a Drop-down List with PHP



PHP


In this tutorial, we will show how to retrieve data a drop-down list form with PHP, to deterine which selection a user has chosen.

Drop-down lists are used with all types of forms in which a user has to select one option.

As an example, below is a questionnaire which uses a drop-down list as a selection method for a number of options. A user can only select one option.

How would you like to pay?




Now that we have this above questionnaire that we want a user to answer, we need a way to extract the data from this form and process it, so that we can know which option the user selected, which translates into which method he will pay by.

And we can do this using PHP.

Select a choice from the drop-down list above and click submit. You will see the update below.

The pay method that you have chose to use is:



Coding
HTML Code
First, the HTML Code to Create the drop-down list above:



When creating a drop-down list in HTML, the select tag is used to enclose all of the options in the drop-down list. The select tag has an opening and closing tag. -The option tag is for each of the individual choices in a drop-down list that a user can select. The option tag contains the "value" attribute. The Value attribute is unique to all of the options. This is the text that is used to differentiate one option from all the rest. -If you want to select an option from the drop-down list by default, you can use the line checked="checked" for the given drop-down option. If you don't select any by default, the first option on the list will be selected by default.

The text you place after the option tag is the text that is shown in the drop-down list options.

In order for PHP to extract the data from the HTML form, the following line of coding is used:

PHP Code

Although the name of the PHP variable doesn't have to be the name of the drop-down list, it is good coding technique to follow this convention. Here, the PHP variable is extracts the data from the drop-down list of the selected option.

For the form above, the PHP code used was:




Related Resources

How to Retrieve Data from a Text Box with PHP

How to Retrieve Data from a Radio Button with PHP

How to Retrieve Data from a Check box with PHP

How to Retrieve Data from an Array of Checkboxes with PHP

How to Retrieve Data from a List Box with PHP

How to Retrieve Data from a Textarea with PHP




HTML Comment Box is loading comments...