Form Validation - radio buttons, check box and drop down

Hello, I am creating a form which sends to an email address but first checks whether the user has inputted information correctly. I am new to php and have tried my best, but I can’t get my mind round this. Please help!
There are 6 questions, 3 of which work (standard text input with email validation), however, I do not know how to incorporate and validate radio buttons, check boxes and drop down options. Can you please help me how to alter the php code to accommodate this? Thank you so much for any help given, I’m under a deadline so a quick response would be greatly appreciated!

HTML code:

[code]
* indicates required field

<tr>
  <td valign="top"> <label for="student">Are you a student? *</label>		 	</td>
  <td valign="top"> <input  type="radio" name="student"> Yes <input  type="radio" name="sudent" checked> No	</td>
</tr>
<tr>
  <td valign="top"> <label for="vehicle">Do you have: *</label>		 	</td>
  <td valign="top"> <input type="checkbox" value="Bike"> A bike<br>
		    <input type="checkbox" value="Motorbike"> A motorbike<br>
    	 	    <input type="checkbox" value="Car"> A car</td>
</tr>
Name *
Age range you fall under? * 21-25 26-30 31-35 36-40
Phone Number *
Email Address *
[/code] PHP code: [php]<?php if(isset($_POST['email'])) { $email_to = "[email protected]"; $email_subject = "Your email subject line";
 function died($error) {
     echo "We are very sorry, but there were error(s) found with the form you submitted. ";
     echo "These errors appear below.<br /><br />";
     echo $error."<br /><br />";
     echo "Please go back and fix these errors.<br /><br />";
     die();
 }

 // validation expected data exists
 if(!isset($_POST['the_name']) ||
     !isset($_POST['phone_number']) ||
     !isset($_POST['email'])) {
     died('We are sorry, but there appears to be a problem with the form you submitted.');       
 }

 $the_name = $_POST['the_name']; // required
 $phone_number = $_POST['phone_number']; // required
 $email_from = $_POST['email']; // required

$error_message = "";
   $string_exp = "/^[A-Za-z .'-]+$/";
   if(!preg_match($string_exp,$the_name)) {
      $error_message .= 'The Name you entered does not appear to be valid.<br />';
   }

$string_exp = “/^[0-9]+$/”;
if(!preg_match($string_exp,$phone_number)) {
$error_message .= ‘The Phone Number you entered does not appear to be valid.
’;
}

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
  $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {
died($error_message);
}

$email_message = "Form details below.\n\n";
 function clean_string($string) {
   $bad = array("content-type","bcc:","to:","cc:","href");
   return str_replace($bad,"",$string);
 }

 $email_message .= "Name: ".clean_string($the_name)."\n";
 $email_message .= "Phone Number: ".clean_string($phone_number)."\n";
 $email_message .= "Email: ".clean_string($email_from)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
‘X-Mailer: PHP/’ . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

Thank you for you for the information

<?php } ?>

Back to form page[/php]

you validate them like any other thing, how you do it depends on the name. You need to first give the radio buttons and checkboxes names.

if they’re named something like

A bike
A motorbike
A car </td

then, the validation would be
if(count($_POST[‘vehicle’]) == 0) {
$vehicle = $_POST[‘vehicle’];
} else {
$error_message .= “Please choose a vehicle type!”;
}

Just remember that its an array, when you go to us it, you need to use a loop. You can do radio buttons the same way, but generally, if there’s just 2 (typically yes and no type things), then you can just give each button a name.

If you don’t want to deal with arrays, then just give each checkbox its own name and validate as such.

Sponsor our Newsletter | Privacy Policy | Terms of Service