2011-05-17 49 views
0

我遇到了一些PHP的麻烦。如何让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

不知道你想要什么,而是指出一些的东西。 1)你有两个标签。 2)你有你的宝贝包在双引号,所以它会被视为字符串发送$ other_color_detail – robx 2011-05-17 23:42:32

回答

1

我有一点很难理解的问题是什么,但如果我理解正确的话,你想有一个嵌套if

$text = ''; 

if ($yes_or_no == 'yes') 
{ 
    // only add $yes_or_no to the text 
} 
else 
{ 
    // add colours[] to text, you might want to add a check to not add 'other_colour' 
    foreach ($colours as $colour) 
    { 
    if ($colour != 'other_colour') 
    { 
     // add $colour to text 
    } 
    } 

    if (in_array('other_colour', $colours) && !empty($other_colour_detail)) 
    { 
    // add other_colour stuff to text 
    } 
} 

// send your text 
+0

哦,亲爱的,对不起,我不太清楚!这是昨天的一个愚蠢的日子,我的脑袋被打破了!这可能是 - 我会给它一个镜头,让你知道! – Martin 2011-05-18 18:15:16