Doubt when picking up checkbox group repeated

1

I am making a registration form where I have all the days 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 Add +1 button where this checkbox group doubles. As a checkbox I put name of it as array and if you have more than one checkbox group I do not know how to do it, since I need to get group by group, that is, if I have a group I will only do one foreach() with PHP and solve the case, since the indexes will be 0 to 6 (because they are 7 days). Now if you have 2 groups, the indexes will be from 0 to 13 and every 7 is a group and I need to divide those groups.

    
asked by anonymous 05.10.2014 / 02:45

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 />";
        }
    }
}
?>
    
06.10.2014 / 07:11