Reputation: 2833
I have the following form:
<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>
If one of either 'Yes' or 'No' are selected then the sub radio buttons show and you can then select one of them.
I have the following simple validation:
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; }
If nothing is selected, I get an error notice as I want.
If 'Yes' or 'No' is selected, but none of the sub radio buttons are selected, I get an error notice, again as I want.
The problem is when 'Not Sure' is selected, I get an error notice because none of the sub radion buttons are selected. I don't want this error.
I only want an error notice if 'Yes' or 'No' have been selected and then none of their sub radio buttons have been selected. If not sure is selected, I want the form to submit without any errors.
I hope I've explained this ok!
Any help would be great.
Upvotes: 0
Views: 196
Reputation: 38436
I would check the Not Sure
radio button before the others and set it in a flag to use in all of your other conditions:
$notSure = (!empty($_REQUEST['answer']) && ($_REQUEST['answer'] == 'Not sure'));
The you can use it with:
if (!$notSure && !@$_REQUEST['answerdetail']) ...
Side Note (not answer-specific)
Using the @
for error suppression can cause your code to run sluggish. For simple tasks as checking if a $_REQUEST
value is set, I would recommend using isset()
or empty()
instead, such as:
if (!$notSure && empty($_REQUEST['answerdetail'])) ...
Upvotes: 1