我有一个基本的 html 表单,我想将其与两个提交按钮一起使用。 CGI 脚本采用表单值并对其进行处理(无论使用哪个提交按钮),但我希望每个按钮稍后在脚本中与不同的操作相关联,即打印不同的输出:
## Get the form value in the usual way.
form = cgi.FieldStorage()
RawSearchTerm = form.getvalue("QUERY")
## Process the form info etc.
if (button1 was pressed):
print this
elif (button2 was pressed):
print this other thing
任何想法表示赞赏,谢谢。
最佳答案
<input type="submit" value="Submit" name="Submit1" />
<input type="submit" value="Submit" name="Submit2" />
这将根据按下的提交按钮为您的表单数据提供不同的名称。
你可以做到
form = cgi.FieldStorage()
if "Submit1" in form:
button = 1
elif "Submit2" in form:
button = 2
else:
print "Couldn't determine which button was pressed."
因为表单就像 Python dict
一样,您可以检查其中是否有给定的名称。它应该包括所有发送的表单实体的名称,因此,对于一个字段和两个提交按钮,它应该包含表单字段的名称和按下的提交按钮的名称。。 p>
关于Python CGI 脚本 : Basic HTML Form With Two Submit Buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6815672/