我正在尝试使用两个不同的提交按钮。如果按下一个提交按钮,则转到一个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>'
答案 0 :(得分:1)
一种解决方案是将表单发布到中间脚本,根据单击的提交按钮决定运行哪个脚本。
因此,如果提供了Submit1
的值,请运行脚本A.如果提供了Submit2
的值,请运行脚本B.
答案 1 :(得分:0)
使用调度脚本。这也允许快速进口。 例如:
...
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 ...