表单提交后PHP重定向 [英] PHP re-direct after form submitted

查看:36
本文介绍了表单提交后PHP重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在人们正确提交表单后将他们重定向到 PDF,在我这样做之前,我对表单字段进行了一些检查以确保它们已正确填写,现在我正在尝试使用header() 来做我的重定向,但是因为在我收到错误之前我已经回显了很多次.下面是我的代码,我该怎么办?

I'm trying to redirect people to a PDF after they've submitted the form correctly, before I do this I do some checking of the form fields to make they've been filled out correctly, now I was trying to use header() to do my re-direct, but because I've echoed a number of times before I get an error. Here's my code below, what can I do?

<?php
    if(isset($_POST['submit'])) {
        if($valid_fname == "Y") {
            if($valid_sname == "Y") {
                if($valid_company == "Y") {
                    if($valid_email == "Y") {                   
                    }
                    else {
                        echo "<p class=\"secText\">Please enter a valid email address</p>\n";
                    }
                }
                else{
                    echo "<p class=\"secText\">Please enter the company you work for</p>\n";
                }
            }
            else {
                echo "<p class=\"secText\">Please enter your surname</p>\n";
            }
        }
        else {
            echo "<p class=\"secText\">Please enter your first name</p>\n";
        }
        if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
            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');
            exit();
        }
    }
?>

EDIT 好的,我最后用一些 javascript 让它工作了

EDIT Ok I got it to work with a bit of javascript in the end

    if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
        echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
        echo "<script type=\"text/javascript\">\n";
        echo    "   <!--\n";
        echo    "       setTimeout(\"window.location = 'http://www.sefasinnovation.co.uk/personal_touch.pdf';\",5000);\n";
        echo    "//-->\n";
        echo    "</script>\n";
    }

推荐答案

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

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.

这篇关于表单提交后PHP重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆