How to Retrieve Data From a Radio Button in PHP 


               


How to Retrieve Data from a Radio Button with PHP



PHP


In this tutorial, we will show you how to retrieve data from a radio button with PHP.

By retrieving data from a radio button, we can know which selection a user selects out of a group of choices.

This can be anything from a quiz to any type of questionnaire where a user has to select one choice out of many.

Below is an example of a questionnaire which uses radio buttons as selection options.

Which Credit Card would you like to use?


MasterCard
Visa
American Express


The credit card you want to use is

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 radio button (credit card) the user selects.

And we can do this using PHP.



Coding
HTML Code
First, the HTML Code to Create the Radio Button Form Above:



When creating a radio button form in HTML, the input tag is used and contains the type, name, and value attributes.

-The type attribute is set to "Radio" for Radio buttons
-The name attribute is set to the collective or group name of all the radio buttons. All the radio buttons in a certain group will all have the same name attribute, which in the example above is "credit_card".
-The value attribute is set to the name of each indivdual element of the radio buttons. In this case, since we are listing credit cards, the values of the attributes are "MasterCard", "Visa", and "American Express". When displaying the data from a selected radio button, it is the value attribute which is shown.
-If you want to select a radio button by default, usually the first one, you add checked="checked" to the first radio button.

The text you place after the input tag is the text that is shown in the radio button options.

In order for PHP to extract the data from the HTML form, the following line of code are used.

PHP Code



Although the name of the PHP variable doesn't have to be the name of the radio button, it is good coding technique to follow this convention. Here, the PHP variable is attracts the data from the radio button form of the selected button. By default, it selects the defaulted select box which is the radio button that has the line checked="checked". However, if the user selects another radio button, this is now the selected button and this will be extracted using the "post" method.

For the form above, the PHP code used was:


If no button is selected by default, meaning you didn't check any radio button with checked="checked", the following line of code is used:


Note: Note that even if the line checked="checked" is added to a radio button, it will be checked by default but it still will not be a selected choice until the user clicks the 'Submit' button of the form. This is why when you first load the page, no credit card is chosen. Only until the submit button is clicked with a radio button selected is one selected.

Related Resources

How to Retrieve Data from a Text Box 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 Drop-down List 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...