1 回答

TA贡献1825条经验 获得超4个赞
您可以为您的复选框使用数组输入名称,然后这将在 PHP 中转换为数组:
foreach (getSundaysForTheMonth($currentYear, $currentMonth) as $sunday) {
$thisSunday = $sunday->format("m - d - Y");
echo "<input type=\"checkbox\" name=\"date[]\" value=\"$thisSunday\">$thisSunday<BR>";
}
这会产生以下输出 ( 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>
在 PHP 中,您将得到一个数组 (in $_POST['date']),它看起来像(例如,如果选中了第一个和第三个复选框):
Array (
[0] => '04 - 05 - 2020'
[1] => '04 - 19 - 2020'
)
请注意,如果您要将这些值插入到数据库中,您应该将它们放入正确的 ISO-8601 格式 ( YYYY-MM-DD),因此将foreach循环更改为如下所示:
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>";
}
- 1 回答
- 0 关注
- 121 浏览
添加回答
举报