2011-05-25 72 views
5

作爲我的第一個Web應用程序,我開發了一個非常簡單的調查。用戶每次刷新頁面時都會詢問用戶隨機提問。答案被髮送到一個CGI腳本使用後保存答案的數據庫。網頁重定向到使用CGI的主頁Python

但是,當用戶按下提交按鈕時,它會自動進入負責處理數據的頁面,並且由於它沒有任何輸出,所以它是空白頁面。現在,如果用戶想回答另一個問題,他們必須按下瀏覽器中的「後退」並刷新頁面,這樣就會彈出一個新問題。我不想要這個。

我想要的方式是,當用戶按下提交時,答案會自動轉到處理腳本,並且頁面會用新問題刷新自己,或者至少在處理後用新問題重定向到主調查頁面。

+0

'meta http-equiv =「refresh」content =「0; url = http://example.com/」>'不是選項? – khachik 2011-05-25 10:28:31

+0

@Khachik:我應該在哪裏放?在標題中? – Hossein 2011-05-25 10:29:22

+0

它在頁面加載後保持刷新。反正有嗎? – 2015-10-14 03:12:41

回答

6

你想實現這一點:https://en.wikipedia.org/wiki/Post/Redirect/Get

這比聽起來要簡單得多。接收POST的CGI腳本必須產生以下輸出:

Status: 303 See other 
Location: http://lalala.com/themainpage 
+0

在哪裏打印這些輸出? – 2015-10-14 03:10:51

+0

雖然這在我的桌面上的Chrome上正常工作,但在我的iPad上出現Safari瀏覽器錯誤,提示「太多重定向」......任何想法?我試圖清空ipad上的緩存..但沒有運氣。 – 2017-09-05 17:43:19

0
<html> 
    <head> 
    <meta http-equiv="refresh" content="0;url=http://www.example.com" /> 
    <title>You are going to be redirected</title> 
    </head> 
    <body> 
    Redirecting... 
    </body> 
</html> 

meta-refresh缺點和替代here

+0

我做到了,但我的網頁在加載後不斷刷新...我希望在表單提交後刷新一次。它甚至在我按下提交按鈕之前刷新。 – Hossein 2011-05-25 10:37:10

+0

@Hossain,可能你會重定向到一個包含該元的頁面。 – khachik 2011-05-25 10:38:51

+0

是真的,問題在主頁面被詢問。提交後我想這個主頁再次刷新(導致一個新的問題彈出) – Hossein 2011-05-25 10:42:54

6

您也可以從處理腳本發送一個HTTP標頭:

Location: /

你處理你的答案後,你會發出上述標題。我會建議你追加一個隨機數查詢字符串。例如Python的例子(假設你使用Python CGI模塊):

#!/usr/bin/env python 
import cgitb 
import random 
import YourFormProcessor 

cgitb.enable() # Will catch tracebacks and errors for you. Comment it out if you no-longer need it. 

if __name__ == '__main__': 
    YourFormProcessor.Process_Form() # This is your logic to process the form. 

    redirectURL = "/?r=%s" % random.randint(0,100000000) 

    print 'Content-Type: text/html' 
    print 'Location: %s' % redirectURL 
    print # HTTP says you have to have a blank line between headers and content 
    print '<html>' 
    print ' <head>' 
    print ' <meta http-equiv="refresh" content="0;url=%s" />' % redirectURL 
    print ' <title>You are going to be redirected</title>' 
    print ' </head>' 
    print ' <body>' 
    print ' Redirecting... <a href="%s">Click here if you are not redirected</a>' % redirectURL 
    print ' </body>' 
    print '</html>' 
+0

可以給我一個更詳細的例子,我完全new.don't知道在哪裏把this.thx – Hossein 2011-05-25 11:27:45