I am trying to use PHP to validate a form on a contact page, but I keep getting valid input required errors although all the boxes have been completed.
This is my code:
[code]<?php
$to = ‘[email protected]’;
$subject = ‘Subject Message’;
$contact_submitted = ‘Message displayed after form successfully validates.’;
function email_is_valid($email) {
return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
}
if (!email_is_valid($to)) {
echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
}
if (isset($_POST['contact_submitted'])) {
$return = "\r\n";
$youremail = trim(htmlspecialchars($_POST['your_email']));
$yourname = stripslashes(strip_tags($_POST['your_name']));
$yoursubject = stripslashes(strip_tags($_POST['your_subject']));
$yourmessage = stripslashes(strip_tags($_POST['your_message']));
$contact_name = "Name: ".$yourname;
$subject_text = "Subject: ".$yoursubject;
$message_text = "Message: ".$yourmessage;
$message = $contact_name . $return . $subject_text . $return . $message_text;
$headers = "From: ".$youremail;
if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yoursubject != "" && $yourmessage != "" ) {
mail($to,$subject,$message,$headers);
$yourname = '';
$youremail = '';
$yoursubject = '';
$yourmessage = '';
echo '<p style="color: blue;">'.$contact_submitted.'</p>';
}
else echo '<p style="color: red;">Please enter your name, a valid email address, a subject, your message before sending your message.</p>';
}
?>[/code]
And the related HTML portion:
[code]
Name
Email Address
Email Address
Message<?php echo $yourmessage; ?>