我有以下表格:
<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; }
如果没有选择任何内容,我会收到错误通知。
如果选择“是”或“否”,但没有选择任何子单选按钮,我会再次收到错误通知。
问题是当选择'Not Sure'时,我收到错误通知,因为没有选择任何子辐射按钮。我不想要这个错误。
如果选择了“是”或“否”,我只想要一个错误通知,然后没有选择任何子单选按钮。如果不确定,我希望表单提交没有任何错误。
我希望我已经解释好了!
任何帮助都会很棒。
答案 0 :(得分:1)
我会先检查Not Sure
单选按钮,然后将其设置为一个标志,以便在所有其他条件下使用:
$notSure = (!empty($_REQUEST['answer']) && ($_REQUEST['answer'] == 'Not sure'));
您可以将它用于:
if (!$notSure && !@$_REQUEST['answerdetail']) ...
旁注(不是特定答案)
使用@
进行错误抑制可能会导致代码运行缓慢。对于检查是否设置了$_REQUEST
值的简单任务,我建议改为使用isset()
或empty()
,例如:
if (!$notSure && empty($_REQUEST['answerdetail'])) ...