且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

表单提交后PHP重定向

更新时间:2022-12-07 10:05:57

您可以使用 输出缓冲仅在发送标头后才发送内容.

You could use output buffering to send content only after the headers have been sent.

if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
    ob_start();
    echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";         
    header('Location:  http://www.sefasinnovation.co.uk/personal_touch.pdf');
    ob_end_flush();
    exit();
}

但是,由于在重定向完成之前几乎没有时间显示消息,因此这种方法将毫无用处.

However since there is practically no time to display the message before the redirection is done this approach would be useless.

您***的方法可能是将您的页面重定向到一个小的 HTML 页面,该页面将在经过一定时间后触发 JavaScript 重定向.这是一般的想法.你应该把细节整理好.

Your best approach here would probably be to redirect your page to a small HTML page which will trigger a JavaScript redirect after a certain amount of time has passed. Here is the general idea. You should sort the details out.

PHP

if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
    header('Location:  http://www.sefasinnovation.co.uk/notification.html');
    exit();

    // You could also avoid redirection to an HTML file and output the code directly
    echo <<<HTML
    <html>
        <head>
        <title>Enter desired title</title>
        <script type="text/javascript">
            setTimeout(function(){
                window.location = "http://www.sefasinnovation.co.uk/personal_touch.pdf";
            }, 5000);
        </script>
        </head>
        <body>
            <p class="secText">Thank you for confirming your details, you will be re-directed to "The Personal Touch" Whitepaper shortly.</p>
            <p>If this page isn't redirected in 5 seconds please click <a href="http://www.sefasinnovation.co.uk/personal_touch.pdf">here</a>.</p>
        </body>
    </html>
HTML;
}

notification.html(上面的PHP代码也可以吐出这段代码,但前提是之前页面上没有输出)

notification.html (the PHP code above could also spit this code out but only if there was not output on the page previously)

<html>
    <head>
    <title>Enter desired title</title>
    <script type="text/javascript">
        setTimeout(function(){
            window.location = "http://www.sefasinnovation.co.uk/personal_touch.pdf";
        }, 5000);
    </script>
    </head>
    <body>
        <p class="secText">Thank you for confirming your details, you will be re-directed to "The Personal Touch" Whitepaper shortly.</p>
        <p>If this page isn't redirected in 5 seconds please click <a href="http://www.sefasinnovation.co.uk/personal_touch.pdf">here</a>.</p>
    </body>
</html>

notification.html 中的附加链接应允许用户在禁用 JavaScript 的情况下进行手动重定向.

The additional link in notification.html should allow users to do a manual redirection in case JavaScript is disabled.