How to Retrieve Data From an Array of Checkboxes In PHP 


               


How to Retrieve Data from an Array of Checkboxes with PHP



PHP


In this tutorial, we will show you how to retrieve data from an array of checkboxes with PHP, to determine which check boxes are clicked and which aren't.

Check boxes are used as selection options for users in questionnaires to determine to a user's response.

Below is an array of checkboxes in HTML which is very common in forms on the web, such as when a user needs to select either none, one, or multiple options from a list of choices:



Aruba
Hawaii
Jamaica

You haven't selected any destination


Coding

To create the array of checkboxes shown above, the HTML code is:



Every form element in HTML must be enclosed by the form tag. So in this case, since we are creating an array of checkboxes, this must be closed in a pair of opening and closing form tags.

After this, we now create the actual array of checkboxes. This is done with a pair of opening and closing option tags for each checkbox. This tag contains the type, name, and value attributes.

-The type attribute must be set to "checkbox".
-The name attribute is the name of the whole array of checkboxes. It must have a name followed by square brackets [ ], which means that is an array. Since the choices in this example involve tropical island destinations, the array is called "Islands[]".
-The Value attribute is unique for each of the array elements. In this case, it is set to the names of the islands.

A submit button, as in all forms, was added below this. And then after, the closing form tag was inserted to close the form.

PHP Code



In the first block of code, we use the $_POST superglobal array to retrieve all data from each of the checkboxes. This is just to retrieve all the data, without knowing which data the user has selected yet.

In the second block of code, we use an if statement with the isset function to determine if the user has selected any options out of the checkboxes. This checks all of the checkboxes to see if any selections have been made. If no options are selected, the array of checkboxes is not set, and, therefore, the user sees the statement, "You haven't selected any destination."

Then we use the foreach loop to go through each and every individual option. This loop will check every check box to see whether it is selected or not. If the check box option is selected, the value attribute of that option is printed using the echo command. If it is not selected, the value is not output.

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

How to Create a Confirmation Page for an HTML Web Form Using PHP

How to Redirect to Another URL with PHP

How to Create a Search Engine Using PHP

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP



HTML Comment Box is loading comments...