两个不同的提交按钮html/cgi表单

2024-04-24 00:48:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试有两个不同的提交按钮。如果按下一个提交按钮,它将转到一个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>'

Tags: textnameinimportform脚本inputhtml
2条回答

使用调度脚本。这也允许快速进口。 示例:

...
print '<form action="dispatch.cgi" method="post">'
print '<input type="submit" value="Submit" name="Submit1" />'
print '<input type="submit" value="Submit" name="Submit2" />'
...

^{pr2}$
# 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 ...

一种解决方案是将表单发布到中间脚本,该脚本根据单击的提交按钮决定运行哪个脚本。在

因此,如果提供了Submit1的值,则运行脚本A。如果提供了Submit2的值,则运行脚本B

相关问题 更多 >