我在使用一些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 时,它会出现,当它被取消选中时,它就会消失。
我希望它做的是这样的:
如果在第一个实例中选择了"是",则忽略所有复选框和文本区域,即使他们首先选择了"否"并在复选框和文本区域中输入了详细信息。
如果已写入other_colour_detail文本区域,但随后取消选中"other_colour",则不会返回"other_colour_detail"文本区域的任何内容
这是 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" ;
谢谢你看一看,
马丁。
选中"否"时,您应该禁用 colors[] 元素。禁用元素不会提交:
<script type="text/javascript">
function toggle_colour_inputs(enabled) {
if ( "yes" ) {
document.form1.colours.disabled=false;
}
else {
document.form1.colours.disabled=true;
}
}
</script>
<input type="radio" name="yes_or_no" id="yes" value="yes" onchange="toggle_colour_inputs(this.value)" />
<label for="yes">yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" onchange="toggle_colour_inputs(this.value)" />
<label for="no">no</label>
<?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') {
if (count($colours) > 0) {
// colours to email
}
} else {
if (($other_colour != '') && ($other_colour_detail != '')) {
// details of other colour
}
}
?>