When a form is submitted, all the form elements that have a name
attribute specified for them submit their name
and their value
. With most form elements, the value
comes from what the user inputs.
When you submit a form that has radio buttons and/or checkboxes in it, only the name/value pairs form the checked checkbox or radio button is submitted. This way, your form processing code doesn't have to sort out which buttons/boxes were checked. A consequence of this however, is that both checkboxes and radio buttons must have a value
set for their value
attribute. This value is how you will know which button/box was selected (receiving a name/value pair of checkbox4=true
doesn't really tell you much.)
In the following code, we will know which checkboxes were checked just by looking at the data submitted to the form's action
which checkboxes were checked and what the meaning of those checks were:
<input type="checkbox" name="chkStudent1" value="Mary"> Mary
<input type="checkbox" name="chkStudent2" value="John"> John
<input type="checkbox" name="chkStudent3" value="Joe"> Joe
Now, when the form is submitted, and let's say you check the second checkbox, the name/value pair of chkStudent2=John
will be submitted. Your form processing code will know exactly which element was checked and the corresponding data will be available to that code.