Help Please

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…

You are always calling your mail function because it’s outside of any if() control structures.

This if


[FONT=Courier New][COLOR=#007700]if(isset([COLOR=#0000bb]$_POST[/COLOR][COLOR=#007700][[/COLOR][COLOR=#dd0000]'submit'[/COLOR][COLOR=#007700]])) {
[/COLOR][/COLOR][/FONT]

ends here


echo '</p><p>Please try again</p><p><br /></p>';
}
} // <---- This ends the if, should it not be lower down under the mail send?

so you are saying that I should get rid of that extra “}”? because it’s now barking this error during run time

Parse error: syntax error, unexpected $end in /homepages/0/d215339618/htdocs/new_website_2/contact.php on line 261

No… I’m saying you should check your code because the mail function is being run after the page loads no matter what happens - did you write the script? It should be obvious where the problem is if you’ve written the whole thing.

Your logic basically has a flaw

you want to nest your if statemtents, so that the next if conditional is only evaluated if the previous one passes. There is another post about this same problem like three threads down.