Skip to content
PHP Logo

How can PHP handle and store multiple checkbox values from a form submission?

When handling multiple checkbox values from a form submission in PHP, you can use an array to store the selected values. This way, you can loop through the array to access each selected checkbox value individually. To do this, make sure the checkbox inputs in your form have the same name attribute with square brackets (e.g., name="checkbox[]").

php
// Assuming the form has checkboxes with the name "checkbox[]"
if(isset($_POST['checkbox'])) {
    $selectedCheckboxes = $_POST['checkbox'];
    
    foreach($selectedCheckboxes as $checkbox) {
        // Process each selected checkbox value here
        echo $checkbox . "<br>";
    }
}