I’m having trouble properly validating radio buttons and passing the selected result to a formmail. Would someone mind taking a look at this and pointing out my mistake?
// 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\
\\r\
";
// $message .= "\\r\
\\r\
$name ($email) wrote: \\r\
\\r\
";
$message .= "DOG APPLICATION FORM \\r\
";
$message .= "1. Name of dog(s) you would like to adopt? \\r\
$dogName \\r\
";
$message .= "2. How did you hear about this particular dog(s)? \\r\
$hearDog";
$message = wordwrap($message, 80);
//build the additional headers
$additionalHeaders = "From: Animal Shelter Contact Page <webmaster@hear.com>\\r\
";
// $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> </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> </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> </td>
</tr>
</table>
<input name="dogApp" type="submit" id="dogApp" tabindex="4" value="Send" />
</form>