Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do html checkboxes only transmit an 'on' value?

Tags:

html

checkbox

I'm just starting to learn internet programming.

After playing around with checkboxes, I found out the hard way, that it is only when the checkbox is checked that it transmits an 'on' value.

Why not also send an 'off' value?

like image 392
Google Avatar asked Nov 23 '10 21:11

Google


People also ask

What value does HTML checkbox return?

Return Value: It returns a string value that represents the value of the value attribute of a input checkbox field.

What value does checkbox submit?

<input type="checkbox">

Does a checkbox need a value?

Rendering Checkboxes Checked By DefaultThere is no required value for the checked attribute. However, per the checkbox specification only an empty value or 'checked' are valid. When checked is added to the checkbox element the browser will render it as selected.


1 Answers

It is possible to have many checkboxes with the same element name:

<form action="order.php" method="get">

  <p>
    <label><input type="checkbox" name="toppings" value="olives"/>Olives</label>
    <label><input type="checkbox" name="toppings" value="mushrooms"/>Mushrooms</label>
    <label><input type="checkbox" name="toppings" value="onions"/>Onions</label>
    <label><input type="checkbox" name="toppings" value="eggplant"/>Eggplant</label>
    etc...
  </p>

  <p><input type="submit" value="Submit"/></p>

</form>

The url will later include only the checked values and will look something like:

order.php?toppings=mushrooms&toppings=onions&toppings=eggplant

This would have not worked well if an "off" value was sent as well.

like image 120
Udi Avatar answered Oct 13 '22 19:10

Udi