【问题标题】:Two different submit buttons html/cgi form两个不同的提交按钮 html/cgi 表单
【发布时间】:2012-12-13 16:44:03
【问题描述】:

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

【问题讨论】:

    标签: python html cgi


    【解决方案1】:

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

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

    【讨论】:

    • 不确定您的确切意思,介意发布一个示例或其他内容吗?谢谢
    • 我不太了解您使用的语言。我刚看到这篇文章,以为我会给我 2 美分。你想清楚了吗?你用了什么代码?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-09
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 2020-11-20
      • 2016-12-01
      相关资源
      最近更新 更多