简体   繁体   English

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

[英]Two different submit buttons html/cgi form

I am trying to have two different submit buttons. 我试图有两个不同的提交按钮。 If one submit button is pressed then it goes to one cgi script and if the other is pressed it goes to another. 如果按下一个提交按钮,则转到一个cgi脚本,如果按下另一个,则转到另一个cgi脚本。 Right now below is my code but it isn't working as I want it to. 现在,下面是我的代码,但它无法正常运行。 They both only go to the same script regardless of which one is pressed. 不管按下哪个脚本,它们都只会进入相同的脚本。

#!/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>'

One solution is to have the form post to an intermediary script that decides which script to run based on which submit button was clicked. 一种解决方案是将表单发布到中间脚本中,该脚本根据单击的提交按钮来决定要运行哪个脚本。

So if the value for Submit1 was provided, run script A. If the value for Submit2 was provided, run script B. 因此,如果提供了Submit1的值,请运行脚本A。如果提供了Submit2的值,请运行脚本B。

Use a dispatch script. 使用调度脚本。 This also allows fast imports. 这也允许快速导入。 Example: 例:

...
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM