简体   繁体   English

Python CGI脚本:具有两个提交按钮的基本HTML表单

[英]Python CGI Script: Basic HTML Form With Two Submit Buttons

I have a basic html form which I want to use with two submit buttons. 我有一个基本的HTML表单,我想与两个提交按钮一起使用。 The CGI script takes the form value and processes it (regardless of which submit button used), but I want each button to be associated with different actions later in the script, ie print different output: 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

Any ideas appreciated, thanks. 任何想法表示赞赏,谢谢。

<input type="submit" value="Submit" name="Submit1" />
<input type="submit" value="Submit" name="Submit2" />

This will give you a different name in your form data depending on which submit button was pressed. 根据您按下哪个提交按钮,这将为您的表单数据提供一个不同的名称。

You can do 你可以做

form = cgi.FieldStorage()

if "Submit1" in form:
    button = 1
elif "Submit2" in form:
    button = 2
else:
    print "Couldn't determine which button was pressed."

because form acts like a Python dict and you can check if a given name is in it. 因为表单的行为就像Python dict ,您可以检查其中是否有给定的名称。 It should include the names of all form entities sent, so, with one field and two submit buttons, it should contain the name of the form field and the name of the submit button that was pressed . 它应该包括所有发送的表单实体的名称,因此,在一个字段和两个提交按钮的情况下,它应该包含表单字段的名称和所按下提交按钮的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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