0

I am having a problem with a contact form that I am creating, I have tried to look up about the same error, I dont seem to have any white spaces and I have read that you should not have an echo before the header tag but as you can see, I need it that way and I have had it working in this order before on another site. The error is:

Warning: Cannot modify header information - headers already sent by (output started at /home/vicarage/public_html/mail.php:8) in /home/vicarage/public_html/mail.php on line 25

Here is the coding for the php file:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Submitting...</title>
</head>
<body>
<?php
$Name = $_POST ['full_name'];
$Email = $_POST['email'];
$Number = $_POST['phone_number'];
$Company = $_POST['company_name'];
$Message = $_POST['message'];
$formcontent="Name: $Name
\n Email: $Email 
\n Number: $Number
\n Company: $Company
\n Message: $Message";
$recipient = "info@vicarage-support.com";
$subject = "Contact";
$mailheader = "From: $Email \r\n";
ini_set("sendmail_from","info@vicarage-support.com");
mail($recipient, $subject, $formcontent, $mailheader) or die("Please try again.");
echo("Form Submitted.");
header("Location:http://www.vicarage-support.com/contact_us.html");
?>
</body>
</html>

Thanks in advance guys.

TristanD27
  • 99
  • 1
  • 22
  • 1
    You cannot use `header()` after you have output anything to the browser (i.e. using `echo`, `print` or outputting HTML at the top of the file like you do here.. – naththedeveloper Jan 15 '14 at 16:15
  • I did see the "Headers already sent by PHP" but I was trying to do it with an echo before redirecting which is why I made a new question, thank you for the suggestion though. – TristanD27 Jan 15 '14 at 16:47

1 Answers1

0

You can't use header() if you've already printed something.

From http://www.php.net/manual/en/function.header.php :

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

AleVale94
  • 727
  • 1
  • 8
  • 14
  • But if I put the echo after the header, will it still display the echo before redirecting back to the website? As you can probably tell, I am getting it to send form info, produce a "Form submitted" and then return to the previous page. Also, do you have any idea of how I can make it wait 3/4 seconds before redirecting back? Thank you for the reply. – TristanD27 Jan 15 '14 at 16:25
  • You can use javascript: http://www.quackit.com/javascript/codes/timed_javascript_redirect.cfm – AleVale94 Jan 15 '14 at 16:40
  • Or you can do it via PHP using a meta tag instead of header function: – AleVale94 Jan 15 '14 at 16:44
  • That's worked perfectly thank you, I took out the header and added the JS after the PHP, I would +1 if I had enough rep. – TristanD27 Jan 15 '14 at 16:45