Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

contact form PHP redirect is not working

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

like image 709
Abel Avatar asked Dec 01 '22 05:12

Abel


1 Answers

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.

like image 112
BudwiseЯ Avatar answered Dec 04 '22 11:12

BudwiseЯ