2011-08-01 18 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上的教程。