Python CGI 脚本 : Basic HTML Form With Two Submit Buttons

标签 python html cgi

我有一个基本的 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/

相关文章:

javascript - jQuery 验证 - 多个输入字段 - 有些是必需的

c - 如何将 STDIN 流更改为二进制

python 简单的 wsgi 文件上传脚本 - 有什么问题?

python - 使用 Python 和 Gmail 作为提供者发送电子邮件的安全方法是什么?

javascript - 我在网页上的下拉框中看不到选项,但在元素中可以看到

python - 带有 web2py 的 SQLFORM.grid 的多行标题

javascript - 动态创建 bootstrap 4 张卡片显示为一列而不是正常布局(行)

Apache 服务器上的 Python CGI

python - 调用函数报错 'not subscriptable'

python - 在python中用参数组装一个长字符串