添加重定向到PHP脚本

时间:2014-03-25 22:13:36

标签: php

我尝试将重定向添加到以下脚本的URL。单击按钮后,我似乎无法使其正常工作。有关如何使其工作的任何建议?提前谢谢!

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");
}

&GT;

4 个答案:

答案 0 :(得分:0)

发出任何内容后,您无法发送标题。所以,在重定向之前没有回声。

答案 1 :(得分:0)

以下是header()的文档。

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

正如PHP所说,你必须在为用户返回任何输出之前设置标题。您必须删除回声才能使其正常工作。

答案 2 :(得分:0)

尝试这样的事情:

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

确保您在网页上有此内容:

<script type="text/javascript">

答案 3 :(得分:0)

您可以使用输出控制功能阻止任何缓冲区。 在这里阅读更多http://www.php.net/manual/en/ref.outcontrol.php

在您的情况下,您只能使用javascript重定向用户,否则您必须重新设计应用程序。

而不是

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

使用此

die ("<script> top.location.href='http://www.example.com' </script>");