-1

I'm trying to add redirection to a URL to the following script. I can't seem to be able to make it work after clicking the button. Any suggestions for how to make it work? Thanks in advance!

if ($Email=="") {
    echo "<div class='alert alert-error'>
            <a class='close' data-dismiss='alert'>×</a>
            <strong>Warning!</strong> Please enter your email.
        </div>

    ";
}   
elseif ($Name=="" or $PhoneDay=="") {
    echo "<div class='alert alert-error'>
            <a class='close' data-dismiss='alert'>×</a>
            <strong>Warning!</strong> Fill all the required fields.
        </div>";
}   
else{
    mail($to, $subject, $msg, "From: $_REQUEST[Email]");
    echo "<div class='alert alert-success'>
            <a class='close' data-dismiss='alert'>×</a>
            <strong>Thank you!</strong>
        </div>";
    header("Location: http://www.example.com");
}

?>

  • Is that all of your code? Does the email send? Do you see your HTML output? – John Conde Mar 25 '14 at 22:14
  • 1
    headers cannot be sent after content has been echo'd out. – cmbuckley Mar 25 '14 at 22:16
  • Oh sorry, yes, the email sends but in the HTML output I get. Warning: Cannot modify header information - headers already sent by (output started at /home/content/52/9448852/html/vision/send_mail.php:42) in /home/content/52/9448852/html/vision/send_mail.php on line 43 – user3461850 Mar 25 '14 at 22:16

4 Answers4

0

You can't send out a header after you have emitted anything. So, no echo before the redirect.

dar7yl
  • 3,727
  • 25
  • 20
0

Here is the documentation for header().

http://www.php.net/manual/en/function.header.php

As PHP puts it, you have to set the headers before you return any output for the user. You have to remove the echo in order for it to work.

0

Try something like this:

 print '<script language="Javascript">document.location.href="http://example.com/";</script>';

Make sure you have this on the web page:

<script type="text/javascript">
0

You can prevent any buffer using Output control functions. Read more here http://www.php.net/manual/en/ref.outcontrol.php

In your case you could only use javascript to redirect user, otherwise you have to redesign application.

Instead of

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

use this

die ("<script> top.location.href='http://www.example.com' </script>");
wake-up-neo
  • 814
  • 7
  • 9