1 回答

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個贊
您可以為您的復(fù)選框使用數(shù)組輸入名稱,然后這將在 PHP 中轉(zhuǎn)換為數(shù)組:
foreach (getSundaysForTheMonth($currentYear, $currentMonth) as $sunday) {
$thisSunday = $sunday->format("m - d - Y");
echo "<input type=\"checkbox\" name=\"date[]\" value=\"$thisSunday\">$thisSunday<BR>";
}
這會產(chǎn)生以下輸出 ( 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 中,您將得到一個數(shù)組 (in $_POST['date']),它看起來像(例如,如果選中了第一個和第三個復(fù)選框):
Array (
[0] => '04 - 05 - 2020'
[1] => '04 - 19 - 2020'
)
請注意,如果您要將這些值插入到數(shù)據(jù)庫中,您應(yīng)該將它們放入正確的 ISO-8601 格式 ( YYYY-MM-DD),因此將foreach循環(huán)更改為如下所示:
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 關(guān)注
- 157 瀏覽
添加回答
舉報