How to Retrieve Data from a Check box with PHP



PHP


In this tutorial, we will show how you can retrieve data from a checkbox using PHP.

Checkboxes are used in questionnaires where a user can select more than one option from a list of choices. Unlike radio buttons which only allows a user to select a single option, check boxes usually allow multiple options to be selected.

As an example, see the below questionnaire which uses checkboxes as selection options.

Which activities would you like to do?

Snorkeling
Scuba Diving
Parasailing


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 checkbox(es) the user selects, which translates into which activities the person wants to do.

And we can do this using PHP.

Select the checkboxes above and click submit. You will see the update below.

The activities you want to do are
No activities chosen yet


Coding
HTML Code
First, the HTML Code to Create the Checkbox Form Above:



When creating a checkbox form in HTML, the input tag is used and contains the type and name attributes.
-The type attribute is set to "Checkbox" for Checkboxes.
-The name attribute is the name of each individual checkbox. This attribute is important because this is the attribute you will use to identify each checkbox and display it if it is checked.
-If you want to select a checkbox by default, you can use the line checked="checked" for the given checkbox. However, in this case, no checkboxes are selected by default.

The text you place after the input tag is the text that is shown in the checkbox 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 radio button, it is good coding technique to follow this convention. Here, the PHP variable is extracts the data from the checkbox form of the selected checkboxes.

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 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...