Retrieve $_POST Data from Multiple Checkboxes
When working with forms that contain multiple checkboxes, it can be essential to retrieve the selected values from within your PHP script. This guide will demonstrate how to extract the checked values for subsequent processing.
In the provided form code, each checkbox is assigned a unique value representing the primary key of a database record ($row['Report ID']). To identify the checked checkboxes, you need to set the name attribute of the form to check_list[]. By doing so, you're creating an array where each element corresponds to a selected checkbox.
Now, you can access the checked values using the $_POST superglobal. The key check_list will return an array of the selected values. Here's an illustrative 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 if (!empty($_POST['check_list'])) { foreach ($_POST['check_list'] as $check) { echo $check; // Echoes the value of each checked checkbox } } ?>
In this example, when the button is clicked, the code will loop through the selected checkbox values and echo each value. If the checkbox with the value $row['Report ID'] was selected, it would be echoed as part of the result.
By leveraging this approach, you can effectively retrieve the IDs of the records you want to delete from your database when the submit button is clicked.
The above is the detailed content of How to Retrieve Selected Values from Multiple Checkboxes in PHP?. For more information, please follow other related articles on the PHP Chinese website!