PDA

View Full Version : Formmail Radio Validation


toad78
01-13-2010, 04:51 AM
Having a little trouble validating the radio buttons in my form. If I don't select a radio button, it posts the error message on the form (which is what it should do). Unfortunately, when I select a radio button, it pops up the error message still and it won't process the form with the radio button selection:

// set flag to indicate whether mail has been sent
$mailSent = false;
if (array_key_exists('dogApp', $_POST)) {
// mail processing script
// remove escape characters from POST array
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
}

// validate the input, beginning with name
$dogName = trim($_POST['dogName']);
if (empty($dogName)) {
$error['dogName'] = 'Please enter the name of the dog(s) you wish to adopt';
}
$hearDog = $_POST['hearDog'];
if (empty($hearDog)) {
$error['hearDog'] = 'Please select how you heard about this dog(s)';
}
// initialize variables
$to = 'hear@gmail.com';
$subject = 'Dog Application';
// build the message
$message = 'On '.date('l, M j, Y').' at '.date('g:ia').', '."\r\n\r\n";
// $message .= "\r\n\r\n$name ($email) wrote: \r\n\r\n";
$message .= "DOG APPLICATION FORM \r\n";
$message .= "1. Name of dog(s) you would like to adopt? \r\n $dogName \r\n";
$message .= "2. How did you hear about this particular dog(s)? \r\n $hearDog";

$message = wordwrap($message, 80);

//build the additional headers
$additionalHeaders = "From: Animal Shelter Contact Page <webmaster@hear.com>\r\n";
// $additionalHeaders .= "Reply-To: $email";

//send the email if there are not errors
if (!isset($error)) {
$mailSent = mail($to, $subject, $message, $additionalHeaders);
// check that the mail was sent successfully
if (!$mailSent) {
$error['notSent'] = 'We\'re sorry, there was a problem sending your mail. Please make another attempt to send your inquiry again. If this problem continues, please contact the webmaster at <a href=\"mailto:webmaster@hear.org\">webmaster@hear.org</a> for further assistance.';
}
}
} <form id="dogApp" name="dogApp" method="post" action="">
<h3>PART 1:</h3>
<table width="98%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><label for="dogName">1. Name of dog(s) you would like to adopt?</label>
<?php if (isset($error['dogName'])) { ?>
<span class="warning"><?php echo $error['dogName']; ?></span>
<?php } ?>
<br />
<input value="<?php if (isset($_POST['dogName'])) { echo $_POST['dogName'];} ?>" name="dogName" type="text" id="dogName" size="50" maxlength="50" /></td>
<td></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label for="hearDog">2. How did you hear about this particular dog(s)?</label> <?php if (isset($error['hearDog'])) { ?>
<span class="warning"><?php echo $error['hearDog']; ?></span>
<?php } ?>
<br />

<input name="hearDog" type="radio" id="Petfinder.org" value="Petfinder.org" <?php
if (!$_POST || (isset($error) && $_POST['hearDog'] == 'Petfinder.org')) { echo
'checked="checked"';
} ?> />
<label for="Petfinder.org">Petfinder.org</label>

<input name="hearDog" type="radio" value="Pets911" id="Pets911" <?php
if (isset($error) && $_POST['hearDog'] == 'Pets911') { echo
'checked="checked"';
} ?> />
<label for="Pets911">Pets911</label>


<input name="hearDog" type="radio" value="Website" id="Website" <?php
if (!$_POST || (isset($error) && $_POST['hearDog'] == 'Website')) { echo
'checked="checked"';
} ?> />
<label for="NoahFriends">Noah's Friends Website</label>

<input name="hearDog" type="radio" value="Breed Rescue Site" id="Breed Rescue Site" <?php
if (!$_POST || (isset($error) && $_POST['hearDog'] == 'Breed Rescue Site')) { echo
'checked="checked"';
} ?> />
<label for="Breed Rescue Site">Breed Rescue Site</label>
</td>
<td></td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><label for="Other"></label>
<input name="hearDog" type="radio" value="Other" id="Other" <?php
if (!$_POST || (isset($error) && $_POST['hearDog'] == 'Other')) { echo
'checked="checked"';
} ?> />
<label for="Other">Other, please explain:</label>
<br />
<textarea name="hearDog" cols="50" rows="7" id="Other"></textarea></td>
<td>&nbsp;</td>
</tr>
</table>
<input name="dogApp" type="submit" id="dogApp" tabindex="4" value="Send" />
</form>