1

I am aware there are many questions on this topic already, but having gone through them I am unable to make sense of them in regards to applying to my own contact form probably due to me being rather new at PHP and the fact that so many people use wordpress and contact form 7 so many answers and search results do not help me

I have a PHP contact form I have made over the months to fit my own requirements however I would like the form to redirect to a thank you page on submission

I have tried adding the following code:

header('Location:  thank-you.php');
exit;

but I think due to the fact that I have headers at the top of my code just below

include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

and then some echos in my form code below I am getting the error that headers have already been sent when I submit the form.

I have tried adding the Location with the headers and replacing the echos with it as I have heard this and white-space in the code can cause this..

Below is my code, with the new lines above added:

The top of my code page:

include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

 $name = ($_POST['name']);
    $email = ($_POST['email']);
    $message = ($_POST['message']);
    $from = ($_POST['email']);
    $to = 'info@mywebsite.co.uk'; 
    $subject = "Enquiry from Visitor " . $name;
    $human = ($_POST['human']);

   $headers = 'From: ' . $email . "\r\n" .
   'Reply-To: ' . $email . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
   header('Location:  thank-you.php');
exit;

This is the code just below the header tag on my form, just before the form starts:

<h2>Contact Us</h2>
<hr width=10%  align=left>

<?php 
    if (isset($_POST['submit']) && $human == '4') {              
    if (mail ($to, $subject, $message, $headers)) { 
    echo '<p>Thanks for getting in touch. Your message has been sent & We will get back to you shortly!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
} 
    } else if (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
?>

I know I am meant to replace the echos with the Location: thank-you.php but surely I still need the echos for if the person missed a required field or answered the anti-spam question incorrectly?

Note: I have tried replacing

echo '<p>Thanks for getting in touch. Your message has been sent & We will get back to you shortly!</p>';

with

header('Location:  thank-you.php');
exit;

but I still get the same error message when I submit the form

I am a newbie to PHP and do not understand how to incorporate a thank you page with my code and whilst keeping the echos for if the form hasn't been filled out correctly

Any help much much appreciated! Thanks :)

ck777
  • 161
  • 1
  • 1
  • 20
  • 1
    Is anything outputted from "header.php"? My advice is to do all your PHP processing before outputting anything to the page. Let PHP figure things out before your write any HTML to the screen. The post marked as a duplicate is worth studying -- it's full of useful info. – showdev May 22 '17 at 23:44
  • Yes as I said above some is at the top of the code, some is below the header tag... If I move it all up to the top it may work? Is the line of code I added in the correct place if so? Thanks for your suggestions.. yes I have seen that page I am just too inexperienced with PHP to fully understand, I keep reading all of the questions and trying different things but I keep getting the same message, sometimes on the page instead of the form and sometimes when I submit the form – ck777 May 22 '17 at 23:50
  • 1
    The basic idea is not to output anything before executing the `header()` command. So, put the code that decides whether to call `header()` first in your file. Note the description at [php.net](http://php.net/manual/en/function.header.php): "It is a very common error to read code with *include*, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called." – showdev May 22 '17 at 23:54
  • hang on, does this mean I cannot have include($_SERVER['DOCUMENT_ROOT']."/includes/header.php"); at the top of my contact form page code? as it says this is where the output already started in the error message.. how do I include the header if this is the case? – ck777 May 22 '17 at 23:54
  • ahh thanks, okay sounds like you have just answered my above question, thank you. So can I put the code include($_SERVER['DOCUMENT_ROOT']."/includes/header.php"); below when the header is called? – ck777 May 22 '17 at 23:55
  • Oh wow it has now worked! Thank you so much. I didn't really understand what it meant by output I didn't realise this counted, thanks a lot and sorry for the stupid question – ck777 May 22 '17 at 23:58
  • 1
    No worries. I know it can be a frustrating problem, but you'll get the hang of it. – showdev May 22 '17 at 23:59
  • Thank you, I do hope so, just need to get a basic understanding and then I at least will be able to use other questions to help me a bit more, so baffling sometimes! Thanks again, I have up-voted your comments :) – ck777 May 23 '17 at 00:19

0 Answers0