Validating Radio Buttons

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>&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>

Hints
Debug using:
echo ‘<pre>’.print_r($_POST, true).‘</pre>’;

When
1> input radio is unchecked
2> input radio is checked

Sorry. I’m not too bright.

Who wrote that much code then? :wink:

If you know how to validate other $_POST variables then it is really easy for radio buttons as well. You can use isset(), empty() functions to check whether the radio button has some value or not.


if(isset($_POST['hearDog']) && !empty($_POST['hearDog'])){
    // radio button is clicked and has some values in $_POST['hearDog']
    // Now you can again check what value is coming with $_POST['hearDog'] and do the needful.
}
else{
    // there is no radio button clicked/selected/checked
}

Thank you rajug:

This is what I have so far. I’m having a little trouble with the development of the ‘Other’ section. Say if someone selects ‘Other’ and it is required to insert info. in the <textarea>, but they don’t, I’m having trouble with my validation working in that department.

Plus, when someone does insert info. in the <textarea>, it doesn’t post. Obviously I’m not writing my validation correctly:

HTML:

<input name="hearDog" type="radio" value="Other" id="Other" <?php 
	if (isset($error) && $_POST['hearDog'] == 'Other') { echo
	'checked="checked"';
} ?>	/>
<label for="Other">Other, please explain:</label>
<br />
<textarea name="hearOther" cols="50" rows="7"><?php if (isset($_POST['hearDog'])) {echo $_POST['hearDog'];} ?></textarea>

PHP: obviously the first statement is checking to see if any of the radio in ‘hearDog’ have been selected. If not, then post the error message. The ‘$hearOther’ is regarding the <textarea> if someone selects ‘Other’ and it’s empty, then post the error message. But when someone does fill it out, and it is sent, the message answer is ‘Other’ with no details. I’m wondering if I need to combine $hearDog/$hearOther in a better if/else statement. But I’m looking for suggestions!

$hearDog = $_POST['hearDog'];
if (empty($hearDog)) {
$error['hearDog'] = 'Please select how you heard about this dog(s)';
}

$hearOther = trim($_POST['hearOther']);
 if (isset($_POST['hearDog']) && $_POST['hearDog'] == 'Other' && empty($hearOther)) {
  $error['hearOther'] = 'Please explain how your heard about this dog';
 }

$message .= "2. How did you hear about this particular dog(s)? $hearDog \\r\
\\r\
";