2011-08-01 21 views
1

我试图在人们提交表单之后将其重定向到PDF,然后执行此操作我对表单字段进行了一些检查,以使其正确填写,现在我试图使用header()来重定向,但是因为在出现错误之前我已经多次回显。以下是我的代码,我该怎么办?PHP在表单提交后重新指向

<?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(); 
     } 
    } 
?> 

编辑好,我得到它与一些JavaScript的工作到底

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"; 
    } 
+2

请详细了解如何使用布尔值并请请学习如何进行负面比较。 '如果(!$ valid_compay)echo'请输入..''更短,更容易阅读! – gnur

回答

1

您只能在发送标头后使用output buffering发送内容。

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

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

这里最好的办法可能是将您的页面重定向到一个小的HTML页面,在经过一段时间后会触发JavaScript重定向。这是一般的想法。你应该对细节进行分类。

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代码上面还可以吐出这段代码出来,但只有当以前有没有在页面上输出)

<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的情况下执行手动重定向。

1

尝试做一个链接将用户重定向到PDF页面,这样的链接包含PDF页面URL

<a href="http://www.sefasinnovation.co.uk/personal_touch.pdf">Thank you for confirming your details </a> 

,或者您可以使用元标签后2秒

将用户重定向
<meta http-equiv="refresh" content="2; url=http://www.sefasinnovation.co.uk/personal_touch.pdf" /> 
0

你的问题是在这里:

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

你不能输出任何东西,如果你想发送重定向报头的页面。删除该echo语句并再次尝试该页面。

或者,您可以使用重定向<meta>标签编写HTML页面(您可以使其延迟5秒钟然后重定向,然后您的消息就可以工作)。

查看Google上的教程。