0

我有以下表格:

<form action="?" method="post" name="contactform">
<?php if(@$errorsAndNotices):?>
<div class="error">
<p><?php echo @$errorsAndNotices; ?></p>
</div>
<?php endif; ?>

<div class="question">
<p><span><input type="radio" name="answer" value="Yes"/></span> Yes</p>
</div>
<div id="one" class="answers" style="display:none">
<p><input type="radio" name="answerdetail" value="Reason One" /> Reason One</p>
<p><input type="radio" name="answerdetail" value="Reason Two" /> Reason Two</p>
</div><!-- end answers -->

<div class="question">
<p><span><input type="radio" name="answer" value="No"/></span> No</p>
</div>
<div id="two" class="answers" style="display:none">
<p><input type="radio" name="answerdetail" value="Reason One" /> Reason One</p>
<p><input type="radio" name="answerdetail" value="Reason Two" /> Reason Two</p>
</div><!-- end answers -->

<div class="question">
<p><span><input type="radio" name="answer" value="Not sure" /></span> Not sure</p>
</div>
<div id="three" class="answers" style="display:none">
<p>No problem, we’ll drop you an email next week.</p>
</div>

<input type="submit" value="" class="submit" />
</form>

如果选择“是”或“否”之一,则显示子单选按钮,然后您可以选择其中一个。

我有以下简单的验证:

orsAndNotices = '';
 if(!@$_REQUEST['answer'])                      { $errorsAndNotices .= "Please select Yes, No or Not sure.<br/>\n"; $nameFail = 1; }
 if(!@$_REQUEST['answerdetail'])                { $errorsAndNotices .= "Please select your answer.<br/>\n"; $emailFail = 1; }

如果未选择任何内容,我会根据需要收到错误通知。

如果选择了“是”或“否”,但没有选择任何子单选按钮,我会再次收到错误通知。

问题是选择“不确定”时,我收到错误通知,因为没有选择子辐射按钮。我不想要这个错误。

如果选择了“是”或“否”,然后没有选择任何子单选按钮,我只想要一个错误通知。如果选择不确定,我希望表单提交时没有任何错误。

我希望我已经解释过了!

任何帮助都会很棒。

4

1 回答 1

1

我会Not Sure在其他人之前检查单选按钮并将其设置在一个标志中以在所有其他条件下使用:

$notSure = (!empty($_REQUEST['answer']) && ($_REQUEST['answer'] == 'Not sure'));

您可以将其用于:

if (!$notSure && !@$_REQUEST['answerdetail']) ...

旁注(不是特定于答案)
使用@for 错误抑制可能会导致您的代码运行缓慢。对于检查是否$_REQUEST设置了值这样的简单任务,我建议使用isset()orempty()代替,例如:

if (!$notSure && empty($_REQUEST['answerdetail'])) ...
于 2012-10-09T14:11:26.343 回答