1

I understand this question has been asked before and people answer with things like header(Location: "link.html"); which is great! but I don't know where to put it and believe me, I've tried!

Basically, I have a very simple PHP code that is designed to send information that has been inputted into a form. The code is as below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<?php
$to='myemail@address.com';
$subject='email subject line';
$qanswer=$_POST['qanswer'];

$FirstName=$_POST['FirstName'];
$Surname=$_POST['Surname'];
$CustomerEmail=$_POST['CustomerEmail'];
$CustomerTelephone=$_POST['CustomerTelephone'];
$PickupLocation=$_POST['PickupLocation'];
$AddressLine1=$_POST['AddressLine1'];
$AddressLine2=$_POST['AddressLine2'];
$TownCity=$_POST['TownCity'];
$County=$_POST['County'];
$Postcode=$_POST['Postcode'];
$Destination=$_POST['Destination'];
$PickupDate=$_POST['PickupDate'];
$PickupTime=$_POST['PickupTime'];
$message="FirstName:  ".$FirstName. "\r\n" . "Surname:  ".$Surname. "\r\n" .     "CustomerEmail:  ".$CustomerEmail. "\r\n" . "CustomerTelephone:  ".$CustomerTelephone.     "\r\n" . "PickupLocation:  ".$PickupLocation. "\r\n" . "AddressLine1:  ".$AddressLine1.     "\r\n" . "AddressLine2:  ".$AddressLine2. "\r\n" . "TownCity:  ".$TownCity. "\r\n" .     "County:  ".$County. "\r\n" . "Postcode:  ".$Postcode. "\r\n" . "Destination:      ".$Destination. "\r\n" . "PickupDate: ".$PickupDate. "\r\n" . "PickupTime: ".$PickupTime;

if ($qanswer==10)
{
mail($to,$subject,$message);
echo 'Thank You! Someone will be in contact shortly to confirm your booking';
}
else
{
echo 'Your booking failed to send! Please ensure you answered the anti-spam question     correctly';
}

?>

</body>

</html>

What I would like to do is when the form has been sent successfully, rather than displaying a blank page with the text "Thank You! Someone will be in contact shortly to confirm your booking" I would like to to redirect to a page called "thankyoupage.html" that is located in the same directory. Likewise, if it fails, I'd like it to redirect to a page called "formsendfailure.html".

halfer
  • 19,824
  • 17
  • 99
  • 186
  • You already gave your answer. –  Apr 27 '14 at 16:28
  • possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) (whether you realise it or not, if it fails, that's why). – AD7six Apr 27 '14 at 16:31

3 Answers3

4

If this page is just for the purposes of sending the mail, then do these things:

  • Remove all the HTML, so you end up with just PHP inside <?php ..?> tags.
  • You will then be free to use header('Location: url.php'); - the problem is likely to be that you had already sent output, and in these conditions you cannot set headers. This is because sending real content forces PHP to finish the headers as they stand, and headers cannot be sent after content.
  • I would then send the success/error condition back in the query string, rather than using echo. So you could redirect to /your/url.php?result=ok if everything went well, or /your/url.php?result=error if not. In the target page, you'll need to detect this and either render a success message ("ok"), a failure message ("error"), or nothing as appropriate.
  • It's worth adding a check to ensure you are actually in a post operation - the URL might still be visited by inquisitive people or hungry search engines, and you don't want to feed either an error message.

You could do the last step in a session variable, but that is a bit more complicated, and probably a bit much for this simple requirement anyway.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Super, I removed all the HTML as suggested and I'm now able to have it direct to the pages I wanted. I'm not sure what you said in your second point but replacing echo with header after removing everything but seemed to do the trick. Thanks very much! – user3578679 Apr 27 '14 at 19:48
  • No problem. The second point was basically if you have already sent HTML (like the chunk from `DOCTYPE` to ``) then it is too late to send headers. In pages where you _might_ redirect (not here where you will always do so) then put the PHP block right at the top of the document, and it will work. – halfer Apr 27 '14 at 19:53
1

Pretty much what Halfer said. The header has to be the first thing you send, ie you can't output anything else before it, otherwise it fails.

Probably the best thing would be to set a value in your if-else like this:

if ($qanswer==10)
{
mail($to,$subject,$message);
$redirect = 0;
//echo 'Thank You! Someone will be in contact shortly to confirm your booking';
}
else
{
$redirect = 1;
//echo 'Your booking failed to send! Please ensure you answered the anti-spam question     correctly';
}

Then at the top of your document the very first thing should be something like:

<?php
    if($redirect==0){
        header("Location: success.php");
    }else{
        header("Location: fail.php");
    }
?>

And then have each page display its own message, ie 'your booking was a success'.

halfer
  • 19,824
  • 17
  • 99
  • 186
Deano
  • 176
  • 1
  • 12
0

You will have to move your form processing to the top of your php document. You have to issue the header redirect before other content has been outputted by php.

Currently everything before your first <?php tag will be outputted to the browser before php gets parsed.

lagbox
  • 48,571
  • 8
  • 72
  • 83