kivy
kivy

Reputation: 1037

Mail Form Redirect Issue

I've already tried a couple of proposed solutions that I found for similar issues, but nothing seems to work out, thus I decided to post my issue as well.

I am fairly new to PHP and have created a simple mail form for my website. When trying to redirect to the home page after submitting the form, the header function doesn't work, but one stays at the blank mail.php page.

I would appreciate any ideas what I am doing wrong or help in this regard.

Here's the code I applied:

<html>
<body>
<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader);
header('Location: myurl.com/websitelocation/'); 
exit;
?>
</body>
</html>

Thanks in advance.

Upvotes: 0

Views: 78

Answers (1)

John Conde
John Conde

Reputation: 219804

You have output before you do your redirect which causes the redirect to fail because you've already sent out your HTTP headers. Once you do you can't send redirect headers.

Your HTML is unnecessary on your code. Remove it and it will work. Another solution, if you need to have HTML for any error messages you may wish to show, is to put the HTML after your PHP code. Just make sure there is not HTML or any whitespace before the PHP code that does your redirect.

Also, if your page is blank you may not have error reporting turned on. Always develop with it turned on and reporting all errors including notices. It will help you catch this error and write better code.

Upvotes: 2

Related Questions