How to define a checkbox for two dimensions?

This page has a list of weeks, the user can check another day for another week.

<input type="checkbox" name="week[]" value="Sun"/>
Sun
<input type="checkbox" name="week[]" value="Mon"/>
Mon
<input type="checkbox" name="week[]" value="Tue"/>
Tue
<input type="checkbox" name="week[]" value="Wed"/>
Wed
<input type="checkbox" name="week[]" value="Thu"/>
Thu
<input type="checkbox" name="week[]" value="Fri"/>
Fri
<input type="checkbox" name="week[]" value="Sat"/>
Sat
<br/>
<br/>


<input type="checkbox" name="week[]" value="Sun"/>
Sun
<input type="checkbox" name="week[]" value="Mon"/>
Mon
<input type="checkbox" name="week[]" value="Tue"/>
Tue
<input type="checkbox" name="week[]" value="Wed"/>
Wed
<input type="checkbox" name="week[]" value="Thu"/>
Thu
<input type="checkbox" name="week[]" value="Fri"/>
Fri
<input type="checkbox" name="week[]" value="Sat"/>
Sat
<br/>
<br/>
.
.
.
<!-- can dynamically add more by javascript -->

      

But after sending, the whole day will be combined into a 1D array

So, I change the week [] to the week [] []

But it still concatenates all the values ​​in a 1D array and each element of that array is an array

How to send two dimensions of a checkbox?

I expect to be able to use this to loop through all the weeks and days, I don't want the sequence code to be short to weeks.

            foreach ($week as $oneWeek)
            {
                echo 'week<br>';
                foreach ($oneWeek as $day)
                {
                    echo $day.'<br>';
                }
            }

      

+3


source to share


2 answers


I suggest you group them like this:

if(isset($_POST['submit'])) {
    $weeks = $_POST['week'];
    // in this way, you could identify the week for each day you checked
}


?>

<form method="POST">
    <label><input type="checkbox" name="week[1][]" value="Sun"/>Sun</label>
    <label><input type="checkbox" name="week[1][]" value="Mon"/>Mon</label>
    <label><input type="checkbox" name="week[1][]" value="Tue"/>Tue</label>
    <label><input type="checkbox" name="week[1][]" value="Wed"/>Wed</label>
    <label><input type="checkbox" name="week[1][]" value="Thu"/>Thu</label>
    <label><input type="checkbox" name="week[1][]" value="Fri"/>Fri</label>
    <label><input type="checkbox" name="week[1][]" value="Sat"/>Sat</label>

    <br/><br/>

    <label><input type="checkbox" name="week[2][]" value="Sun"/>Sun</label>
    <label><input type="checkbox" name="week[2][]" value="Mon"/>Mon</label>
    <label><input type="checkbox" name="week[2][]" value="Tue"/>Tue</label>
    <label><input type="checkbox" name="week[2][]" value="Wed"/>Wed</label>
    <label><input type="checkbox" name="week[2][]" value="Thu"/>Thu</label>
    <label><input type="checkbox" name="week[2][]" value="Fri"/>Fri</label>
    <label><input type="checkbox" name="week[2][]" value="Sat"/>Sat</label>

    <br/>

    <input type="submit" name="submit" />
</form>

      



Or that:

$days = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
?>

<form method="POST">
    <?php for($i = 1; $i <= 3; $i++): ?> <!-- print week 1 to week 3 -->
        <?php foreach($days as $day): ?>
            <label><input type="checkbox" name="week[<?php echo $i; ?>][]" value="<?php echo $day; ?>"/><?php echo $day; ?></label>
        <?php endforeach; ?>
        <br/><br/>
    <?php endfor; ?>

    <br/>

    <input type="submit" name="submit" />
</form>

      

+1


source


You need to add a number to the first index.

First week:

<input type="checkbox" name="week[1]" value="Sun"/>

      



Second week

<input type="checkbox" name="week[2]" value="Sun"/>

      

0


source







All Articles