Should checkbox inputs have the same `name` attribute?

Hi all,
In the HTML section of the Curriculum “Basic HTML and HTML5: Create a Set of Checkboxes”, it is stated that:
“All related checkbox inputs should have the same name attribute”

You can not submit the answer successfully unless you give the same name “personality” to all the checkboxes.

Now, I am aware of that for type=“radio” attribute, as only one radio button can be checked, but why should the same apply to checkboxes even though one can choose more than one checkbox?

I do not remember I have seen this before. Other sources give different names for the checkbox input elements.

1 Like

Hi @Atheer, welcome to the forums.

That was for the lesson. The name attribute is not required but it could be helpful in the back end processing. And no, the name attribute doesn’t always have to be the same but it helps to group similar things.
Let’s say you have a form and one section of it has checkboxes for different desserts. You could give selections with a name attribute of ‘fruit’ and then have choices for ‘apple’ and ‘banana’ and selections with a name attribute of ‘pie’ and have choices for ‘apple’ and ‘blueberry’

1 Like

Thanks for the reply,

I still do not understand how is it ok to give more than one checkbox the same name. That is, what the value of name="" will be then if the user checked more than one box. For instance in PHP, what will the value of “personality” in $_POST[“name”] be if the user selected both of the boxes below?
<input type=“checkbox” name=“personality” value=“loving”> Loving
<input type=“checkbox” name=“personality” value=“lazy”> Lazy

See Using checkbox inputs to see how they get passed to the server. Each server-side language has a way of capturing these values for the same name. You can google how to do this for a particular language. For example searching for “php get multiple checkbox values” provides many links with good answers for how to do this in PHP.

2 Likes

Thanks

The mozilla article states that adding brackets at the end of the name (in our example it will be name=“personality[]”) allows the values to be sent as an array to the server-side, but then drops these brackets in later examples.

An answer from a page they linked states that these brackets [] are needed only in PHP