[英]Two different submit buttons html/cgi form
我試圖有兩個不同的提交按鈕。 如果按下一個提交按鈕,則轉到一個cgi腳本,如果按下另一個,則轉到另一個cgi腳本。 現在,下面是我的代碼,但它無法正常運行。 不管按下哪個腳本,它們都只會進入相同的腳本。
#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
keyword = form.getvalue('keyword')
keyset = set(x.strip() for x in open('keywords.txt', 'r'))
print 'Content-type: text/html\r\n\r'
print '<html>'
print "Set = ", keyset
print '<h1>If your keyword is in the set, use this submission button to retrieve recent tweets</h1>'
print '<form action="results.cgi" method="post">'
print 'Keyword: <input type="text" name="keyword"> <br />'
print '<input type="submit" value="Submit" name="Submit1" />'
print '</html>'
print 'Content-type: text/html\r\n\r'
print '<html>'
print '<h1>If your desired keyword is not in the set, use this submission button to add it</h1>'
print '<form action="inlist.cgi" method="post">'
print 'Keyword: <input type="text" name="keyword"> <br />'
print '<input type="submit" value="Submit" name="Submit2" />'
print '</html>'
一種解決方案是將表單發布到中間腳本中,該腳本根據單擊的提交按鈕來決定要運行哪個腳本。
因此,如果提供了Submit1
的值,請運行腳本A。如果提供了Submit2
的值,請運行腳本B。
使用調度腳本。 這也允許快速導入。 例:
...
print '<form action="dispatch.cgi" method="post">'
print '<input type="submit" value="Submit" name="Submit1" />'
print '<input type="submit" value="Submit" name="Submit2" />'
...
#!/usr/bin/env python
# dispatch.py (or dispatch.cgi)
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
if form.getvalue('Submit1'):
import results # result.py: imports are precompiled (.pyc) and decent
result.handle_cgi(form) #OR: execfile('results.cgi')
else:
import inlist
inlist.handle_cgi(form) #OR: execfile('inlist.cgi')
# results.py (or results.cgi)
import cgi
def handle_cgi(form):
keyword = form.getvalue('keyword')
print 'Content-type: text/html'
print
print '<HTML>'
print "Keyword = ", keyword
#...
if __name__ == '__main__':
handle_cgi(globals().get('form') or # don't build / read a POST FS twice
cgi.FieldStorage())
# inlist.py ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.