2012-12-13 74 views
0

我想要有两个不同的提交按钮。如果一个提交按钮被按下,那么它将转到一个cgi脚本,如果另一个按钮被转到另一个。现在下面是我的代码,但它不工作,因为我想要它。无论按下哪一个脚本,它们都只能使用相同的脚本。两个不同的提交按钮html/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>' 

回答

1

一种解决方案是有形式的帖子决定哪些脚本中介脚本来运行基于其提交按钮被点击。

所以,如果是提供Submit1值,运行脚本A.如果提供了Submit2值,运行脚本B.

+0

不知道你的意思究竟是什么,心中张贴一个例子什么的?谢谢 – Neemaximo

+0

我真的不知道你使用的语言。我只是跑过这个帖子,以为我会给我2美分。你弄明白了吗?你使用了什么代码? –

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