Contact Form Issue?

Yes when I add the if, else the page just goes to the PHP page. I don’t know that I should set many conditions anyway yet because I don’t have modal pop ups for error messages yet. I haven’t had a chance to look at Git yet, and all of the projects like PHP Mailer.

I’m not sure what you mean by that. The if-else clause is in the PHP page, but surely it’s the form action that decides whether or not the form goes to that page? So are you saying that if you remove the if-else clause, the form doesn’t submit and the PHP code doesn’t run? That sounds strange, I don’t think I’m understanding that correctly.

My view tends to be the opposite - get the PHP code working and the email sending successfully, then worry about getting the fancy pop-up error messages working.

1 Like

No the code works without the if, else statement. However when I add the if, else and press submit the HTML page redirects to the PHP page I designated within the form, and remains on the page, and does not redirect to where I specified or send an email message. I tried this code

<?php
if (isset($_POST['email']))
    
{

	
		$name = $_POST['name'];
		$visitor_email = $_POST['email'];
		$message = $_POST['message'];

		$email_from = 'Website';

        $email_subject = "New Form Submission";

        $email_body = "User Name: $name.\n".
                       "User Email: $visitor_email.\n".
                        "User Message: $message.\n";

$to = "email@gmail.com";

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
    
header("Location: contact.html")


}

else{
  

header("Location: index.html")

}

This code does the same thing

<?php
 if($_SERVER['REQUEST_METHOD'] === 'POST'){
   // Parse the form data here...


	
		$name = $_POST['name'];
		$visitor_email = $_POST['email'];
		$message = $_POST['message'];

		$email_from = 'Website';

        $email_subject = "New Form Submission";

        $email_body = "User Name: $name.\n".
                       "User Email: $visitor_email.\n".
                        "User Message: $message.\n";

$to = "email@gmail.com";

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
    
header("Location: contact.html")


}

else{

header("Location: index.html")

}

?>

Do you have error reporting disabled? I’m thinking that these two lines

header("Location: contact.html")
...
header("Location: index.html")

should report errors because of the missing semi-colon at the end.

You still have this error, too, where you don’t set a from-address in your email:

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

but I don’t know if that would stop it being sent - maybe it would default to something, and maybe your email server would send it anyway.

When you add the if-else clause into the code, you are making sure that there is nothing - no blank space or empty lines - before the opening <?php tag, aren’t you? Otherwise you’ll get “headers already sent” errors.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.