Skip to content
PHP Logo

How can dynamic checkbox names in PHP forms be effectively processed and accessed after form submission?

Dynamic checkbox names in PHP forms can be effectively processed by using array notation in the name attribute of the checkboxes. This allows multiple checkboxes with dynamic names to be grouped together under a single array variable in the $_POST superglobal array. After form submission, you can access the values of the checkboxes by looping through the array variable.

php
<form method="post">
    <input type="checkbox" name="checkboxes[]" value="option1">
    <input type="checkbox" name="checkboxes[]" value="option2">
    <input type="checkbox" name="checkboxes[]" value="option3">
    <input type="submit" name="submit" value="Submit">
</form>

<?php
if(isset($_POST['submit'])) {
    if(isset($_POST['checkboxes'])) {
        foreach($_POST['checkboxes'] as $checkbox) {
            echo $checkbox . "<br>";
        }
    }
}
?>