0

I'm trying redirect users to a thank you page after submitting a form but my header location doesn't seem to work (local or online). Nothing happens when clicking the submit button (the email is sent though).

My php looks like this :

<?php

$val= $_POST['val'];
$toemail='mail@mail.com';
$name = $val['name'];
$societe = $val['societe'];
$email = $val['email'];
$phone = $val['phone'];
$msg = $val['msg'];

$subject = 'Contact';

$headers = "From: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";

$message = "<b>Nom : </b>".$name."<br>";
$message .='<b>Societe : </b>'.$societe."<br>";
$message .='<b>Email : </b>'.$email."<br>";
$message .='<b>Telephone : </b>'.$phone."<br>";
$message .='<b>Message : </b>'.$msg;

mail($toemail, $subject, $message, $headers);

header("Location: http://example.com/thankyou.html");

?>

What am I doing wrong? Thanks in advance for your help.

Edit: Thanks for your help. If I turn error reporting I get: Warning : Cannot modify header information - headers already sent by (output started at /path/email.php:12)

onerkript
  • 57
  • 11

4 Answers4

2

As others have mentioned, the script is sending output before attempting to send the header. The simplest way to fix it is probably to buffer the output of email() and then send the header, as follows:

ob_start();
mail($toemail, $subject, $message, $headers);
ob_end_clean();

header("Location: http://example.com/thankyou.html");
Juan
  • 53
  • 4
0

Problem is, you are sending output before redirect.

Either use output buffering or do it on a new page.

http://ee1.php.net/manual/en/ref.outcontrol.php

Limiter
  • 497
  • 3
  • 11
0

very simple use following method ob_start(); exit(header("Location: http://example.com/thankyou.html")); Functions that send/modify HTTP headers must be invoked before any output is made. Otherwise the call fails.

Sakthi Karthik
  • 3,105
  • 4
  • 25
  • 29
-4

Using header() you can redirect only to a page within the same site.

you can try using

header("Location: /thankyou.html");

Hope it helps.

Virus
  • 2,445
  • 1
  • 14
  • 17