第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP使用foreach獲取復(fù)選框的值

PHP使用foreach獲取復(fù)選框的值

PHP
墨色風(fēng)雨 2022-12-11 10:16:37
我有一個包含日期的復(fù)選框列表。這是我到目前為止的預(yù)覽:// 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")        );    }這就是我顯示它的方式: // 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++;    }這個想法是用 foreach 而不是這種方式來做:<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>現(xiàn)在我正在嘗試獲取這些值。我認(rèn)為代碼應(yīng)該看起來與此類似但有點(diǎn)不同,因?yàn)檩斎朊Q具有不同的名稱(date0、date1、date2,...)。<?phpif (isset($_POST['date'])) {    foreach ($date as $sunday){        echo $sunday."<br />";        // Store $sunday in an array    }} else {    echo "No selections";}?>關(guān)于如何使這項(xiàng)工作有任何想法?我的目標(biāo)是將它存儲在一個數(shù)組中,其中將被放入數(shù)據(jù)庫中。謝謝。
查看完整描述

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>";

}


查看完整回答
反對 回復(fù) 2022-12-11
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號