0

I have a list of check boxes containing dates. Here's a preview of what I have so far:

// This code gets all the Sunday's in a month:
 function getSundaysForTheMonth($y, $m)
    {
        return new DatePeriod(
            new DateTime("first sunday of $y-$m"),
            DateInterval::createFromDateString('next sunday'),
            new DateTime("last day of $y-$m 23:59:59")
        );
    }

This is how I'm displaying it:

 // Get current Year and Month
    $currentYear = date('Y');
    $currentMonth = date('m');

    // Get month name
    $beginMonthName = date("F", mktime(0, 0, 0, $currentMonth, 10));


    echo "Select which Sunday(s) of the month of ". $beginMonthName . ". $currentYear . " ": \n<BR>";
    $i=0;

    // Display all Sundays for 3 months
    foreach (getSundaysForTheMonth($currentYear, $currentMonth) as $sunday) {
        $thisSunday = $sunday->format("m - d - Y");
        echo "<input type=\"checkbox\" name=\"date".$i."\" value=\".$thisSunday. \">".$thisSunday."<BR>";
        $i++;
    }

The idea was to do it with a foreach instead of this way:

<input type="checkbox" name="date0" value="2020-04-12 ">2020-04-12<BR>
<input type="checkbox" name="date1" value="2020-04-12 ">2020-04-19<BR>
<input type="checkbox" name="date2" value="2020-04-12 ">2020-04-26<BR>
<input type="checkbox" name="date3" value="2020-04-12 ">2020-03-03<BR>

Now I'm trying to get those values. I'm thinking the code should look similar to this but a bit different, since the input name has different names (date0, date1, date2, ...).

<?php

if (isset($_POST['date'])) {

    foreach ($date as $sunday){
        echo $sunday."<br />";
        // Store $sunday in an array
    }
} else {
    echo "No selections";
}
?>

Any ideas on how I can make this work?
My goal is to store it in an array, in which will be put into a database. Thanks.

1 Answer 1

1

You can use an array input name for your checkboxes, this will then get converted into an array in PHP:

foreach (getSundaysForTheMonth($currentYear, $currentMonth) as $sunday) {
    $thisSunday = $sunday->format("m - d - Y");
    echo "<input type=\"checkbox\" name=\"date[]\" value=\"$thisSunday\">$thisSunday<BR>";
}

This produces the following output (demo):

<input type="checkbox" name="date[]" value="04 - 05 - 2020">04 - 05 - 2020<BR>
<input type="checkbox" name="date[]" value="04 - 12 - 2020">04 - 12 - 2020<BR>
<input type="checkbox" name="date[]" value="04 - 19 - 2020">04 - 19 - 2020<BR>
<input type="checkbox" name="date[]" value="04 - 26 - 2020">04 - 26 - 2020<BR>

And in PHP you will get an array (in $_POST['date']) that looks like (if for example the first and third checkboxes were selected):

Array (
    [0] => '04 - 05 - 2020'
    [1] => '04 - 19 - 2020'
)

Note that if you are going to insert these values into a database you should put them into proper ISO-8601 format (YYYY-MM-DD) so change your foreach loop to look like:

foreach (getSundaysForTheMonth($currentYear, $currentMonth) as $sunday) {
    $thisSunday = $sunday->format("m - d - Y");
    echo "<input type=\"checkbox\" name=\"date[]\" value=\"" . $sunday->format('Y-m-d') . "\">$thisSunday\n<BR>";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.