Home > Backend Development > PHP Tutorial > How to Retrieve and Display Checked Checkbox Values from an HTML Form Submission?

How to Retrieve and Display Checked Checkbox Values from an HTML Form Submission?

DDD
Release: 2024-12-10 13:03:10
Original
467 people have browsed it

How to Retrieve and Display Checked Checkbox Values from an HTML Form Submission?

Retrieving Checkbox Values on Form Submission

When working with forms that contain checkboxes, it's essential to retrieve their checked values for processing or storage. This article provides a comprehensive guide to retrieving checkbox values on submission.

The HTML form provided includes seven checkboxes, each representing a different color:

<form action="third.php" method="get">
    <!-- Choices -->
    Red     <input type="checkbox" name="color[]">
Copy after login

The key to capturing checkbox values is using a name attribute with brackets [], indicating that it is an array. This is essential because checkboxes allow for multiple selections.

In the third.php file, you can access the selected checkbox values using the $_GET variable:

$color = $_GET['color'];
Copy after login

However, attempting to print the $color variable directly will result in a "Array to string conversion" notice because it is an array of checked values.

Solutions for Retrieving Checked Values

1. Using foreach Loop

To print each checked color, you can use a foreach loop:

<?php
$color = $_GET['color'];

echo 'The colors you checked are: <br>';
foreach ($color as $color) {
    echo $color . '<br>';
}
?>
Copy after login

This will output each checked color on separate lines.

2. Adding an Empty Array Check

To handle the case where no checkboxes are checked, you can add an empty array check:

<?php
$color = $_GET['color'];

if (isset($_GET['color'])) {
    echo 'The colors you checked are: <br>';
    foreach ($color as $color) {
        echo $color . '<br>';
    }
} else {
    echo 'No colors were checked.';
}
?>
Copy after login

3. Displaying as a List

If you want to display the checked colors as a list, you can use HTML markup within the foreach loop:

<?php
$color = $_GET['color'];

if (isset($_GET['color'])) {
    echo '<ul>';
    foreach ($color as $color) {
        echo '<li>' . $color . '</li>';
    }
    echo '</ul>';
} else {
    echo 'No colors were checked.';
}
?>
Copy after login

By implementing these solutions, you can effectively retrieve and display the checked checkbox values when the form is submitted.

The above is the detailed content of How to Retrieve and Display Checked Checkbox Values from an HTML Form Submission?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template