3

This page has a list of week, user can check different day for different 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 submit, the all day will be combined in a 1D array

So I change week[] to week[][]

But it still combine all value in a 1D array, and each element of this array is an array

How to submit two dimension checkbox?

I expect I can use this to loop through all weeks and days, I don't want to hard code sequence number to weeks.

            foreach ($week as $oneWeek)
            {
                echo 'week<br>';
                foreach ($oneWeek as $day)
                {
                    echo $day.'<br>';
                }
            }
3
  • can you paraphrase your question? and whats the final output? if that goes into 1 dimensional only, how are you going to distinguish which week is it
    – Kevin
    Commented Sep 14, 2014 at 11:28
  • In this HTML, you can see two lists of checkbox for two weeks, if user checked both Sun, how can I know which Sun for which week?
    – CL So
    Commented Sep 14, 2014 at 11:34
  • yes thats my point, you can't group them like that (1D), since there is no identifier which both sundays belong in their respective week
    – Kevin
    Commented Sep 14, 2014 at 11:35

2 Answers 2

1

I suggest you group them this way:

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 this:

$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>
2
  • how to access it back after submit? to be specific from as post? need to loop again and get all?
    – DevC
    Commented Sep 22, 2016 at 12:07
  • @DevC just access the post values, your selected values should be inside it. its an array so most likely you'll access it using a loop
    – Kevin
    Commented Sep 22, 2016 at 22:53
0

You'll need to add a number in the first index.

First week:

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

Second week

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.