<input type="checkbox" name="name" value="any_value" checked>
Attribute | Description |
---|---|
Name | Specifies the name of the checkbox, through which it can be referenced. |
value | Value of the checkbox . Each checkbox will have different values in a group. |
Checked | Checkbox will be selected ( by default ) . In a group of buttons more than one can be selected. |
<input type="Checkbox" name="blue" value="yes" Checked>Blue
<input type="Checkbox" name="red" value="yes">Red
<input type="Checkbox" name="green" value="yes">Green
Using this here is the DEMO : <h1 class="text-primary">Display the Checkbox Selection Values</h1>
<form action="test6.php" method="post" class="mt-4">
<!-- Checkboxes for color selection -->
<input type="Checkbox" name="blue" value="yes" Checked>Blue
<input type="Checkbox" name="red" value="yes">Red
<input type="Checkbox" name="green" value="yes">Green
<!-- Submit button -->
<button type="submit" class="btn btn-primary mt-3">Submit</button>
</form>
test6.php
<h2 class="text-success">Selected Colors:</h2>
<p>
<?php
// Suppress errors for undefined indexes
error_reporting(E_ERROR | E_PARSE);
// Display checkbox values
echo " Red : " . $_POST['red'];
echo "<br>";
echo " Blue : " . $_POST['blue'];
echo "<br>";
echo " Green : " . $_POST['green'];
echo "<br>";
?>
</p>
This PHP code retrieves the values of selected checkboxes from a form using the $_POST array. It suppresses errors for undefined indexes to handle cases where some checkboxes might not be selected. Each checkbox value is displayed with a corresponding label (Red, Blue, Green) using the echo statement.
Tip: To deepen your understanding, explore more about form handling in PHP, including working with the $_POST and $_GET superglobals, validating inputs, and securely managing user data.
HTML Form Drop down Listbox Radio buttons