Got a little problem. I’m doing for validation for a website. Every time i click the contact link to get to the form, it shoots me an email without hitting the submit button. Especially when i don’t enter any data and the validation barks at me, it shoots me a message when i hit the submit button. Here is the code…
<?php
$emailPattern = '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i';
$to = "atlnycdude23@gmail.com";
//$to ="email@email.com // ----> Use this $to email declation just in case in a emergency.
$subject = 'Feedback Wesley Foundation';
$from = 'KSU Wesley Foundation';
if(isset($_POST['submit'])) {
$errors = array();
if(empty($_POST['name'])){
$errors[] = 'You forgot to enter your name.';
}
if(empty($_POST['reason'])){
$errors[] = 'You forgot to select your subject';
}
if(empty($_POST['email'])){
$errors[] = 'You forgot to enter your email.';
}
if(empty($_POST['phone'])){
$errors[] = 'You forgot to enter your phone.';
}
if(empty($_POST['comments'])){
$errors[] = 'You forgot to enter your comments.';
}
if(empty($errors)){
echo '<h1 id="mainhead">Thanks!</h1>
<p>Your feedback has been succussfully sent to us, we look forward of seeing you soon.</p><p><br/></p>';
include ('./includes/footer.html');
} else {
echo '<h1 id="mainhead">Oppps!</h1>
<p class="error">The following error(s) occurred:<br/>';
foreach ($errors as $msg){
echo " - $msg<br />
";
}
echo '</p><p>Please try again</p><p><br /></p>';
}
}
$name = safe (stripslashes( $_POST['name']) );
$reason = safe (stripslashes( $_POST['reason']) );
$email = safe( $_POST['email'] );
$phone = safe($_POST['phone'] );
$classification = safe($_POST['classification']);
$comments = safe (stripslashes( $_POST['comments'] ));
$headers = "From: ". $from . "<" . $to. ">
";
$headers .= "Reply-To: " . $email . "
";
$headers .= "Return-path: ". $email;
$message = "Subject: " . $reason . "
";
$message .= "Name: " . $name . "
";
$message .= "Email: " . $email . "
";;
$message .= "Phone: " . $phone . "
";
$message .= "Year in School: " . $classification . "
";
$message .= "Comments: " . $comments . "
";
if (mail($to,$subject,$message,$headers))
{
/* echo "&Result=success";
} else {
echo "&Result=error"; */
}
function safe($string)
{
$pattern = "/\r|
|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i";
return preg_replace($pattern, '', $string);
}
?>
Please help, thanks…