我對某些PHP有些麻煩。2個選項,PHP忽略第二個細節,如果第一個被選中,反之亦然
這裏是HTML的一個縮短的版本:
<label for="yes_or_no">would you like to tell me your favourite colours?</label>
<input type="radio" name="yes_or_no" id="yes" value="yes" />
<label for="yes">yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" />
<label for="no">no</label>
<div id="colours_box">
<label for="colours">great! please select from the following list:</label>
<input type="checkbox" name="colours[]" id="blue" value="blue" />
<label for="blue">blue</label>
<input type="checkbox" name="colours[]" id="yellow" value="yellow" />
<label for="yellow">yellow</label>
<input type="checkbox" name="colours[]" id="red" value="red" />
<label for="red">red</label>
<input type="checkbox" name="colours[]" id="green" value="green" />
<label for="green">green</label>
<input type="checkbox" name="colours[]" id="other_colour" value="other_colour" />
<label for="other_colour">other_colour</label>
<div id="other_colour_box">
<textarea name="other_colour_detail" id="other_colour_detail"></textarea>
</div>
</div>
的colours_box DIV是隱藏的,在選擇時#NO出現,當使用一些基本的JavaScript選擇#yes消失。 other_colour_box DIV也會做類似的事情 - 默認情況下它是隱藏的,當它出現時檢查#other_colour,當它未被選中時它會消失。
我想它做的是這樣的:
如果在第一時間選擇了「是」,所有的複選框,textarea的被忽略,即使他們選擇了「否」第一,輸入詳細信息複選框和textarea。
如果other_colour_detail textarea的已寫入,但 'other_colour' 隨後被選中,不會返回任何的 'other_colour_detail' textarea的
這裏的PHP:
$yes_or_no = $_POST['yes_or_no'] ;
$colours = $_POST['colours'] ;
$other_colour_detail = $_POST['other_colour_detail'] ;
$colours_to_email .= implode("\n\t", $colours) ;
if (($yes_or_no == 'no') && ($colours != "")) {
$colours_to_email ;
}
if (($yes_or_no == 'no') && ($other_colour != "") && ($other_colour_detail != "")) {
$details_of_other_colour = ":\n\t$other_colour_detail" ;
}
這然後會通過電子郵件反饋給我這樣的事情:
"Did they want to tell me which colours they preferred?\n" .
"$yes_or_no-\t" . "$colours_to_email" .
"$details_of_other_colour" ;
感謝您的關注,
Martin。