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
Name * | |
Age range you fall under? * | 21-25 26-30 31-35 36-40 |
Phone Number * | |
Email Address * |
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]