Retrive Checked Values from Multiple Checkboxes in PHP
In PHP, capturing the values of multiple selected checkboxes in a single form poses no problem. To achieve this, follow these steps:
1. Set the Form's Name Attribute:
Assign a unique name attribute to the form itself, ensuring it ends with the [] suffix to indicate that it expects multiple values.
2. Set Checkbox Names and Values:
For each checkbox in the form, specify distinct names and values. In your provided code, the name attribute is set to check_list, and the values mirror the corresponding Report ID values from your database.
3. Retrieve Checked Values:
In the PHP script, access the checked values using the $_POST superglobal array. The checkbox values will be accessible as an array under the name specified in the form, in this case, $_POST['check_list'].
4. Process Checkbox Values:
Within a loop, iterate through the $_POST['check_list'] array, accessing each checked checkbox's value. In your scenario, these values represent the Report IDs of the messages to be deleted.
Example Code:
<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; // Echo the value for each checked checkbox (e.g., "value 1", "value 3", etc.). // Alternatively, perform your desired action on the checked values, such as deleting the relevant messages. } } ?>
The above is the detailed content of How to Retrieve Values from Multiple Checked Checkboxes in PHP?. For more information, please follow other related articles on the PHP Chinese website!