Doubt when picking up repeated checkbox group

Asked

Viewed 384 times

1

I am making a registration form where I have every day of the week to select

<input type="checkbox" name="dias[]" value="Segunda"> Seg
<input type="checkbox" name="dias[]" value="Terça"> Ter
<input type="checkbox" name="dias[]" value="Quarta"> Qua
<input type="checkbox" name="dias[]" value="Quinta"> Qui
<input type="checkbox" name="dias[]" value="Sexta"> Sex
<input type="checkbox" name="dias[]" value="Sábado"> Sáb
<input type="checkbox" name="dias[]" value="Domingo"> Dom

The problem is that the form has a button Add +1 where duplicates this checkbox group. As are checkbox I put the name of him as array and if you have more than one checkbox group I don’t know how to pick up, because I need to pick up group by group, ie if I have a group I will only do one foreach() with PHP and solve the case, because the indices will be from 0 to 6 (because they are 7 days). Now if there are 2 groups, the indices are 0 to 13 and every 7 is a group and I need to divide these groups.

1 answer

1

You have to input as a multidimensional array: name="dias[0][]":

<form action="test.php" method="post">
    <input type="checkbox" name="dias[0][]" value="Segunda"> Seg
    <input type="checkbox" name="dias[0][]" value="Terça"> Ter
    <input type="checkbox" name="dias[0][]" value="Quarta"> Qua
    <input type="checkbox" name="dias[0][]" value="Quinta"> Qui
    <input type="checkbox" name="dias[0][]" value="Sexta"> Sex
    <input type="checkbox" name="dias[0][]" value="Sábado"> Sáb
    <input type="checkbox" name="dias[0][]" value="Domingo"> Dom
    <input type="checkbox" name="dias[1][]" value="Segunda"> Seg
    <input type="checkbox" name="dias[1][]" value="Terça"> Ter
    <input type="checkbox" name="dias[1][]" value="Quarta"> Qua
    <input type="checkbox" name="dias[1][]" value="Quinta"> Qui
    <input type="checkbox" name="dias[1][]" value="Sexta"> Sex
    <input type="checkbox" name="dias[1][]" value="Sábado"> Sáb
    <input type="checkbox" name="dias[1][]" value="Domingo"> Dom
    <input type="submit" />
</form>
<?php
if( !empty( $_POST['dias'] ) ) {
    foreach( $_POST['dias'] as $key => $value ) {
        echo "<br />Semana $key<br />";
        foreach( $value as $dias ) {
            echo "$dias<br />";
        }
    }
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.