Reputation: 594
I im trying to redirect to my homepage after submitting a message on my contact form, the form sends the email but I get this message:
Array
(
[name] => Abdo
[company] => Mediabyrån A&B
[email] => [email protected]
[content] => Hejsan
[contact_to] => [email protected]
)
Warning: Cannot modify header information - headers already sent by (output started at /customers/4/5/a/webelite.se/httpd.www/kontakt.php:3) in /customers/4/5/a/webelite.se/httpd.www/kontakt.php on line 39
My contact form;
<form action="kontakt.php" method="post">
<p><input type="text" required="required" id="name" name="name" class="text_input" size="22" />
<label for="name">Namn *</label></p>
<p><input type="text" required="required" id="company" name="company" class="text_input" size="22" />
<label for="company">Företag *</label></p>
<p><input type="email" required="required" id="email" name="email" class="text_input" size="22" />
<label for="email">Epost *</label></p>
<p><textarea required="required" name="content" class="textarea" cols="30" rows="5"></textarea></p>
<p><button type="submit" class="button white"><span>Skicka</span></button></p>
<input type="hidden" value="[email protected]" name="contact_to"/>
</form>
and this is my PHP:
<?php
echo $name = $_POST['name'];
echo $company = $_POST['company'];
echo $email = $_POST['email'];
echo $content = $_POST['content'];
$mail_to = '[email protected]';
$subject = 'Lilla form'.$name;
$body_message = 'From: '. $name . "\n";
$body_message .= 'company: '. $company . "\n";
$body_message .= 'E-mail: '. $email ."\n";
$body_message .= 'Message: '. $content;
$headers = 'From: '. $mail_name . "\r\n";
$headers .= 'Reply-To: '. $email ."\r\n";
$success = mail($mail_to, $subject, $body_message, $headers);
echo "<pre>";
print_r($_POST);
header('Location:mydomain');
?>
I also tried using if
($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=YOUR_PAGE_HERE.html\">";
This worked but I got a ugly half a second flash between hitting submit and being redirected.
All help is appriciated.
Thank you
Upvotes: 4
Views: 1565
Reputation: 1826
You can't output anything to the screen before redirecting.
Remove all your echo
'es and print_r
's and you should be fine.
EDIT:
Also as @jhonraymos mentioned, be sure to use header()
properly. Maybe you changed this to hide your actual page you're redirecting to. Either add a local file with correct path definitions or if redirecting to an other domain, you need to define the full url. See Uniform resource locator if in doubt.
Another EDIT:
I see you updated your question. Stop trying indian magic, such as
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=YOUR_PAGE_HERE.html\">";
Just accept the fact not to output ANYTHING to the screen before headers()
and your soul is safe forever. This is not the place to mix http-meta in. PHP can do this just fine.
This might sound as a limitation first, but believe me, it isn't. It's a blessing.
Upvotes: 4
Reputation: 5283
this is error
print_r($_POST);
header('Location:mydomain');
You are printing something before the header("location: mydomain.com")
Upvotes: 4
Reputation: 628
This is very common problem in php. some ways to sort out:
use on top of the page.
check if you accidentally add some white spaces before the php open tag or after the php closed tag.
if still not solve, use java-script window.location instead of header.
Hope it help. Happy coding!!
Upvotes: 2
Reputation: 4634
yes, you are not supposed to echo, print anything before header
try placing this at the top of your page:
<?php ob_start(); ?>
then at the bottom of the page place :
<?php ob_end_flush(); ?>
Upvotes: 3
Reputation:
Headers are used to communicate with your client's browser. They're like little commands that the browser will perform when received. When you output any data (text,numbers,whatever), you're client's browser will print that data. After that, the browser will no longer be interested in any headers you send.
The header()
function is a function used to send custom headers. So when this function is called, headers are sent out to your client's browser.
Now you have a very brief understanding of what it is you're actually trying to do, you should be able to see where your problem lies.
You are outputting other data before sending those custom headers. This is what's triggering the error.
So this:
echo "<pre>";
print_r($_POST);
Should not be before this:
header('Location:mydomain');
Upvotes: 4
Reputation: 329
What the
echo "<pre>";
print_r($_POST);
is for ?
Your redirection may happen before any echo function is called, no ?
Upvotes: 2
Reputation: 10356
You get this warning because you output to the screen your variables' values and redirecting with header
after that.
Headers cannot be sent after your print something (echo
, print_r
...).
In order to fix it , follow the next code:
$name = $_POST['name']; //no echo
$company = $_POST['company'];//no echo
$email = $_POST['email'];//no echo
$content = $_POST['content'];//no echo
$mail_to = '[email protected]';
$subject = 'Lilla form'.$name;
$body_message = 'From: '. $name . "\n";
$body_message .= 'company: '. $company . "\n";
$body_message .= 'E-mail: '. $email ."\n";
$body_message .= 'Message: '. $content;
$headers = 'From: '. $mail_name . "\r\n";
$headers .= 'Reply-To: '. $email ."\r\n";
$success = mail($mail_to, $subject, $body_message, $headers);
//echo "<pre>";
//print_r($_POST);
Upvotes: 1