An HTML checkbox lets a user turn an independent option on or off. A form can contain several checkboxes, and the user may select any number of them unless your application adds other rules.
<input type="checkbox" id="newsletter" name="newsletter" value="yes">
<label for="newsletter">Receive newsletter</label>
If the checkbox is selected when the form is submitted, its name and value are included in the submitted data. An unchecked checkbox is normally omitted entirely.
false. It usually means that no name/value pair is sent for that checkbox.<input type="checkbox" name="name" value="any_value" checked>
The checked attribute is optional. If it is present, the checkbox starts selected.
| Attribute | Purpose |
|---|---|
type="checkbox" | Creates a checkbox control. |
name | Defines the field name used when the selected checkbox is submitted. |
value | Defines the value sent when the checkbox is selected. |
id | Uniquely identifies the control and can connect it to a label. |
checked | Starts the checkbox in the selected state. |
disabled | Disables interaction and normally excludes the checkbox from form submission. |
required | Requires that checkbox to be checked for browser constraint validation. |
value, browsers normally submit the default value on when it is checked. Supplying an explicit value usually makes server-side handling clearer.checked is a Boolean HTML attribute. Its presence is what matters.
<input type="checkbox" id="blue" name="blue" value="yes" checked>
<label for="blue">Blue</label>
The user can normally change the state before submitting the form.
Consider:
<input type="checkbox" name="newsletter" value="yes">
If it is checked, the submission contains a value equivalent to:
newsletter=yes
If it is unchecked, the browser normally sends no newsletter field at all.
false.Use a <label> for each checkbox. For a related set, use <fieldset> and <legend> to describe the group.
<fieldset>
<legend>Choose colours</legend>
<input type="checkbox" id="colour_blue" name="blue" value="yes">
<label for="colour_blue">Blue</label>
<input type="checkbox" id="colour_red" name="red" value="yes">
<label for="colour_red">Red</label>
</fieldset>
Labels enlarge the clickable area and make the controls easier to understand for assistive technologies.
The original Plus2net example uses a different field name for each independent colour choice:
<input type="checkbox" id="blue2" name="blue" value="yes" checked>
<label for="blue2">Blue</label>
<input type="checkbox" id="red2" name="red" value="yes">
<label for="red2">Red</label>
<input type="checkbox" id="green2" name="green" value="yes">
<label for="green2">Green</label>
This approach is convenient when every checkbox represents a separate Boolean property.
Checkboxes do not always need different names. When several controls represent one multi-value field, they can share one logical name.
For PHP, array-style names are convenient:
<fieldset>
<legend>Choose colours</legend>
<input type="checkbox" id="c_blue" name="colors[]" value="blue">
<label for="c_blue">Blue</label>
<input type="checkbox" id="c_red" name="colors[]" value="red">
<label for="c_red">Red</label>
<input type="checkbox" id="c_green" name="colors[]" value="green">
<label for="c_green">Green</label>
</fieldset>
If the user chooses blue and green, PHP receives those selected values as entries in the colors array.
<input type="checkbox" id="premium" name="premium" value="yes" checked disabled>
<label for="premium">Premium option</label>
A disabled checkbox cannot normally be changed by the user and is not included in normal form submission, even if it appears checked.
| Checkbox | Radio button |
|---|---|
| Each choice can be selected independently. | A same-named radio group allows one selected option at a time. |
| Useful for zero, one or several selections. | Useful when exactly one choice from a group is appropriate. |
| Related checkbox values may be submitted under separate names or one multi-value name. | Radio buttons in one group use the same name. |
See HTML radio buttons.
The original Plus2net demo uses three independent checkbox names and submits them to html_frmcheck2.php.
<form action="html_frmcheck2.php" method="post">
<fieldset>
<legend>Choose colours</legend>
<input type="checkbox" id="blue3" name="blue" value="yes" checked>
<label for="blue3">Blue</label>
<input type="checkbox" id="red3" name="red" value="yes">
<label for="red3">Red</label>
<input type="checkbox" id="green3" name="green" value="yes">
<label for="green3">Green</label>
<button type="submit">Submit</button>
</fieldset>
</form>
The old example suppressed undefined-index errors. A better approach is to test whether each checkbox was submitted.
<?php
$red=isset($_POST['red']) && $_POST['red'] === 'yes';
$blue=isset($_POST['blue']) && $_POST['blue'] === 'yes';
$green=isset($_POST['green']) && $_POST['green'] === 'yes';
echo 'Red: ' . ($red ? 'selected' : 'not selected') . '<br>';
echo 'Blue: ' . ($blue ? 'selected' : 'not selected') . '<br>';
echo 'Green: ' . ($green ? 'selected' : 'not selected') . '<br>';
For the array-style colors[] example, validate selected values against the set your application allows:
<?php
$allowed=[
'blue',
'red',
'green'
];
$selected=$_POST['colors'] ?? [];
if(!is_array($selected)){
$selected=[];
}
$selected=array_values(
array_intersect($selected, $allowed)
);
See PHP form handling for receiving and validating submitted form data.
Unchecked checkboxes are normally omitted from the submitted data. Check for the field's presence on the server.
A checked checkbox without an explicit value normally submits on. Use an explicit value when the application expects a specific value.
Associate each checkbox with a label so the option is easier to understand and select.
Different names work well for independent Boolean fields. Related multi-value choices can share one logical field name, such as PHP's colors[].
Disabled controls are normally excluded from form submission.
Because unchecked fields may be absent, test for their presence instead of hiding notices or warnings.
Yes. Checkboxes are independent unless application logic imposes another rule.
The checked Boolean attribute makes the checkbox selected when the page initially loads.
Normally no field is sent for that checkbox at all.
When checked, browsers normally submit the default value on.
Yes. Related multi-value controls can share one logical name. In PHP, array-style names such as colors[] are commonly used.
No. Disabled form controls are normally excluded from submission.
Checkboxes allow independent selections. Radio buttons sharing one name represent mutually exclusive choices where one option is selected at a time.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.