2011-05-18 88 views
0

我对某些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。

回答

0

当选择“否”时,应该禁用颜色[]元素。未提交禁用元素:

<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> 
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 
    } 
} 
?> 
相关问题