开发者

How do I get PHP to ignore de-selected subcategories, even if they contain content?

I'm having some trouble with some PHP.

Here's a shortened version of the HTML:

<label for="yes_or_no">would you like to tell me your favourite colours?&开发者_开发百科lt;/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>

The colours_box DIV is hidden and appears when #no is selected and disappears when #yes is selected using some basic JavaScript. The other_colour_box DIV does a similar thing- it's hidden by default and when #other_colour is checked it appears, when it's unchecked it disappears.

What I'd like it to do is this:

  • if 'yes' is selected in the first instance, all the checkboxes and textarea are ignored, even if they selected 'no' first and entered details to the checkboxes and textarea.

  • if the other_colour_detail textarea has been written in but 'other_colour' has subsequently been unchecked, nothing is returned for the 'other_colour_detail' textarea

Here's the 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" ;
}

This would then feed back to me via email something like this:

"Did they want to tell me which colours they preferred?\n" .
"$yes_or_no-\t" . "$colours_to_email" .
"$details_of_other_colour" ;

Thanks for having a look,

Martin.


I'm having a bit of a hard time understanding what the problem is, but if I understand you correctly, you want a nested 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜