PHP: Submit Form with many Checkboxes to Script comfortably
Tip by Stefan Trost | Last update on 2022-12-29 | Created on 2013-01-08
Every time we have a form with a large number of checkboxes, the question arises, how we can best evaluate them in PHP after a user input. Finally, it can be very complicated to name and read out each box individually in order to get the value of the checkbox.
In this tip I want to show you one way how to get this problem under control and how to set the first steps for an easier evaluation later already in the HTML form.
The HTML Code
First we take a look at the HTML code of our form:
<form method="post" action="script.php"> <input type="hidden" name="colorform" value="1"> <label><input type="checkbox" name="colors[]" value="green">green</label> <label><input type="checkbox" name="colors[]" value="blue">blue</label> <label><input type="checkbox" name="colors[]" value="red">red</label> <label><input type="checkbox" name="colors[]" value="black">black</label> <label><input type="checkbox" name="colors[]" value="white">white</label> <input type="submit" name="submitbutton"> </form>
Our form consists of a submit button and examplary of 5 checkboxes with which color names can be selected.
The special thing: All checkboxes are named with the same name "colors[]". This makes it possible that all values from the checkboxes are sent to the PHP script in only one single variable, the array "colors" and not in many individual variables, as it would be the case if we would give each checkbox a unique name.
If no checkbox is checked, the array is empty. Therefore, we additionally use a hidden field (here with the name "colorform" in the second line), which is sent in any case with a non empty value in order to verify that the form was submitted.
The PHP Code
Lastly, we only need the PHP code to get the data:
if ($_POST["colorform"]) { $colors = $_POST["colors"]; echo '<p>The following colors have been choosen:</p>'; echo '<p>'; foreach ($colors as $value) { echo "$value <br>"; } echo '</p>'; }
In the first line, we check first whether the form was submitted (our hidden field exists). If so, we output a list of the selected colors.
For this purpose, we store the array from the form in the variable $colors and we are using foreach to get through the entire array, so that we can echo the individual values.
Which values are arriving the script, are determined in our HTML code. That are the values written in the attribute "value" of the selected checkbox. Of course, you can also write numbers or any other words into the value.
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile
Related Topics
HTML Form: Redirection depending on Radiobutton or Checkbox State
Tutorial | 0 Comments
jQuery: Toggle Checkboxes (Select/Unselect All)
Tip | 0 Comments
jQuery: Submit complete form and receive content with Ajax
Tutorial | 0 Comments
jQuery: Disable Submit Button if no Checkbox is selected
Tutorial | 7 Comments
JavaScript: Catch Submit of Form
Tutorial | 0 Comments
Send HTML5 Canvas as Image to Server
Tutorial | 0 Comments
Send Form Input as an Array to PHP Script
Tip | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
Hey, this is great, works like a charm.
But I need a checkbox array for a list of contract options which an applicant picks as part of an application form that gets emailed to me.
How can I get it to send the values to my mailbox?
2014-01-21 at 11:24
The easiest way is to store the values in a string instead of echo them.
So, in the loop, use something like that:
After that, you can email the values via the PHP mail function:
You don't need to adjust anything else.
2014-01-21 at 13:52