PHP redirect contact form not working

I try to redirect to my home page after sending a message in my contact form, the form sends an 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: it is not possible to change the header information - headers already sent (the output started with / customers / 4/5 / a / webelite.se / httpd.www / kontakt.php: 3) to / 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 an ugly half second of flash between pressing the submit button and the redirect.

All help is appreciated.

thanks

+4
source share
7 answers

You cannot display anything before redirecting.

Remove all your echo 'es and print_r and everything will be fine.

EDIT:

Also as @jhonraymos mentioned, be sure to use header() correctly. You may have changed this to hide the actual page you are redirecting to. Either add a local file with the correct route definitions, or if redirecting to another domain, you need to determine the full URL. See Unified Resource Locator if in doubt.

Another EDIT:

I see that you have updated your question. Stop trying Native American magic, for example

 if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=YOUR_PAGE_HERE.html\">"; 

Just accept not to display ANYTHING before headers() , and your soul is safe forever. This is not a place to mix http-meta. PHP can do this just fine.

It may sound like a limitation in the first place, but believe me, it is not. This is a blessing.

+4
source

this is mistake

 print_r($_POST); header('Location:mydomain'); 

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

+4
source

Headers are used to communicate with your client browser. They look like small commands that the browser will execute when they are received. When you output any data (text, numbers, etc.), the Client browser will print this data. After that, the browser will no longer be interested in any headers you send.

The header() function is the function used to send custom headers. Therefore, when this function is called, headers are sent to your client browser.

Now you have a very brief idea of โ€‹โ€‹what you are actually trying to do, you should be able to see where your problem is.

You send other data before sending these custom headers. This is what causes the error.

So this is:

 echo "<pre>"; print_r($_POST); 

Should not be before this:

 header('Location:mydomain'); 
+4
source

yes, you should not echo, type anything before header try placing this at the top of the page:

 <?php ob_start(); ?> 

then at the bottom of the page:

 <?php ob_end_flush(); ?> 
+3
source

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

Your redirection can happen before calling any echo function, no?

+2
source

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

  • use at the top of the page.

  • check if you accidentally added some spaces before the open php tag or after the closed php tag.

  • if still not solved, use java-script window.location instead of the header.

Hope this helps. Happy coding!

+2
source

You will receive this warning, because after that you display the values โ€‹โ€‹of your variables and redirect them using header .

Headers cannot be sent after your print ( echo , print_r ...). To fix this, follow the following 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); 
+1
source

Source: https://habr.com/ru/post/1439661/


All Articles