Home >Backend Development >PHP Tutorial >How Can I Retrieve Values from Multiple Selected Checkboxes Using PHP's $_POST?

How Can I Retrieve Values from Multiple Selected Checkboxes Using PHP's $_POST?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-17 10:24:26985browse

How Can I Retrieve Values from Multiple Selected Checkboxes Using PHP's $_POST?

Retrieving $_POST Data from Multiple Checkboxes

When working with forms containing multiple checkboxes, it becomes essential to identify which checkboxes have been selected. This article will guide you through a method to retrieve the values of checked checkboxes using PHP's $_POST superglobal.

To retrieve the selected checkbox values, you must first set the name attribute of each checkbox to an array, like "check_list[]". This modification allows your script to access all the checkboxes as an array in $_POST['check_list'][] after form submission.

Consider the following form example:

<form action="test.php" method="post">
    <input type="checkbox" name="check_list[]" value="value 1">
    <input type="checkbox" name="check_list[]" value="value 2">
    <input type="checkbox" name="check_list[]" value="value 3">
    <input type="checkbox" name="check_list[]" value="value 4">
    <input type="checkbox" name="check_list[]" value="value 5">
    <input type="submit">
</form>

PHP Code to Retrieve Checked Checkbox Values:

if (!empty($_POST['check_list'])) {
    foreach ($_POST['check_list'] as $check) {
        echo $check; // Echoes the value of each checked checkbox
    }
}

In your specific case, replace the "value" attributes with your $row['Report ID'] to retrieve the primary keys of the checked messages for deletion purposes.

The above is the detailed content of How Can I Retrieve Values from Multiple Selected Checkboxes Using PHP's $_POST?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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