-4

I'm trying to build a form using php error checking on the same page.

Everything seems to be working fine except redirecting visitors to another page on my website (thanks.html) after they've filled in the form correctly. First an email is sent to an email address provided at the top of my code and after that visitors should be redirected to the thankspage, which I can't get to work.

The redirecting is this part in my code:

/* Redirect visitor to the thank you page */ header('Location: thanks.html');

Here's what I have in total:

<?php
/* Set e-mail recipient */
$myemail = "myemail@gmail.com";
if (!$_POST['submit']) 
{ 
form();
} else { 
if (empty($_POST['name'])) { $error0='<br>name'; }
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email_address'])) {     $error2='<br>Valid email address'; }
if (empty($_POST['phonenumber'])) { $error1='<br>phonenumber'; }
if (empty($_POST['comment'])) { $error3='<br>comment'; }
$error_messages = $error0.$error1.$error2.$error3;
if ($error_messages) 
{ 
echo "Please ensure the following fields are completed before submitting your form:<strong>". $error_messages     ."</strong><br><br>";
form();
} else { 
$name = $_POST['name'];
$email_address = $_POST['email_address'];
$phonenumber = $_POST['phonenumber'];
$comment = $_POST['comment'];
/* Message for the e-mail */
$message = "
New contact request:
Name: $name
Email address: $email_address
Phonenumber: $phonenumber
comment: $comment
";
$subject = "Contact";
$headers = "From: $email_address";

/* Send the message using mail() function */
mail($myemail, $subject, $message, $headers);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
}}
?>

When checking for errors I get this result:

Warning: Cannot modify header information - headers already sent by (output started at form.php:11). 

Form.php:11 is this line of code:

if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email_address'])) { $error2='<br>Valid email address'; }

So after viewing other posts on stackoverflow I guess the issue might be whitespace or a header that has already been sent. But I still I can't it to work.

Could somebody help out and give a suggestion to fix this?

Chazz
  • 305
  • 3
  • 12

2 Answers2

2

You are correct - You can't redirect if you've output anything to the browser. This goes for spaces at the beginning, anything really.

To get around this, check out the ob_* functions on php.net. Specially, check out flush. You'll be able to bypass this.

Hope that helps. Post again if you have ob_* issues.

Dan Joseph
  • 109
  • 1
  • 7
0

As a side note, eregi has be deprecated as of 5.3.0 so its probably not a good idea to use it. Instead of using

eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email_address'])

use

filter_var($_POST['email_address'], FILTER_VALIDATE_EMAIL)

see http://php.net/manual/en/function.filter-var.php for more info.

Regards

Rijndael
  • 3,683
  • 2
  • 24
  • 26