我在使用某些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被选中时,它会出现,当它被取消选中时它会消失。
我希望它能做到的是:
如果&#39;是&#39;在第一个实例中被选中,所有复选框和textarea都被忽略,即使他们选择了“没有”#39;首先输入详细信息到复选框和textarea。
如果已写入other_colour_detail textarea但是&#39; other_colour&#39;随后未经检查,“#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" ;
感谢您一看,
马丁。
答案 0 :(得分:0)
选中“否”时,应禁用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>
答案 1 :(得分:0)
<?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
}
}
?>