LeBlaireau
LeBlaireau

Reputation: 17467

php redirect issue after email sent

I am getting the old header problem just cant get it to redirect how do I fix it?

Warning: Cannot modify header information - headers already sent

<?php if (isset($_POST['email']))
{
  $content = '<html><body>';
  $content .= 'Email Address: '.$_POST['email'].'<br />';
  $content .= 'Enquiry Type: '.$_POST['enquiry'].'<br />';
  $content .= 'Message: '.$_POST['message'].'<br />';
  $content .= 'Telephone Number: '.$_POST['telephone'].'<br />';
  $content .= 'Preferred Contact Method:" '.$_POST['contact-method'] ;
  $content .= '</body></html>';
  $to = 'myemail*email.com';

$subject = 'Website Contact Form ';

$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $content, $headers)) {
header('Location:form-submitted.php');
    }

}
?>
<!DOCTYPE html>
<html>

Upvotes: 0

Views: 3738

Answers (7)

William R.
William R.

Reputation: 165

Here's the explanation: Remove everything that might me printing to the website that is before the header("Location: site.php"). This is because http headers is always at the top of an request or response. This means that if you echo or print anything to the response before the header is set results in something like this:

HTTP/1.1 404 Not Found
Date: Sun, 18 Oct 2012 10:36:20 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 230
<p>this tag should not be here</p>
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML>
<html>
<head>
   <title>My site</title>
</head>
<body>
   <h1>Hello</h1>
   <p>Lorem ipsum</p>
</body>
</html>

The <p> element as you can see above, may have been printed out with php, and have now corrupted the http response. Anything you want to set with the header(); function will be throwing errors.

I hope this helped :)

Upvotes: 0

Abed Putra
Abed Putra

Reputation: 1215

Use JS

if (mail($to, $subject, $content, $headers)) {   
?>
<script>window.location.replace("http://your url/");</script>
<?php 
}

Upvotes: 0

Imran
Imran

Reputation: 396

//Put this code at the top of your page
<?php ob_start(); ?>

Your code.....

//Put this code at the bottom of your page
<?php ob_flush(); ?>

Upvotes: 0

Adrian
Adrian

Reputation: 544

put this line below:

 ob_start();

at the beginning of executed PHP file works for me.

Upvotes: 0

Vishnu Sureshkumar
Vishnu Sureshkumar

Reputation: 2316

It means that header is already been sent. Please try this

echo "<meta http-equiv='refresh' content=\"0; url=form-submitted.php\">";

Upvotes: 1

Rohan Kumar
Rohan Kumar

Reputation: 40639

You have an invalid email-id

$to = 'myemail*email.com';

Should be

$to = '[email protected]';// @ in place of *

So, mail will return false in above case.

Upvotes: 0

martian
martian

Reputation: 20

Use exit(); After header function.

header('Location:form-submitted.php');
exit();

Upvotes: 0

Related Questions