contact form PHP redirect is not working

走远了吗. 提交于 2019-12-01 19:51:13

问题


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] => a.el-madhoun@hotmail.com
    [content] => Hejsan
    [contact_to] => info@web.se
)

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="info@web.se" 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 = 'info@webelite.se';
$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


回答1:


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.




回答2:


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');



回答3:


this is error

print_r($_POST);

header('Location:mydomain');

You are printing something before the header("location: mydomain.com")




回答4:


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(); ?>



回答5:


What the echo "<pre>"; print_r($_POST); is for ?

Your redirection may happen before any echo function is called, no ?




回答6:


This is very common problem in php. some ways to sort out:

  1. use on top of the page.

  2. check if you accidentally add some white spaces before the php open tag or after the php closed tag.

  3. if still not solve, use java-script window.location instead of header.

Hope it help. Happy coding!!




回答7:


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 = 'info@webelite.se';
$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);


来源:https://stackoverflow.com/questions/12884612/contact-form-php-redirect-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!