找出调用 CGI 脚本的事件
我想要的是能够让我的 CGI 脚本根据启动脚本调用的操作执行不同的操作。
例如,如果按下一个按钮,数据库就会被清除。如果按下另一个按钮,则会提交表单并将该数据添加到数据库中。
我是否应该做一些事情,例如将表单/按钮的名称添加到在 jQuery 中提交的 POST
数据的末尾,然后在脚本中将其关闭?
或者是否有一些其他数据已在 POST
中发送,我可以从 FieldStorage
获取这些数据,这些数据将为我提供决定脚本在调用时应执行的操作所需的信息?
如果我没有使用 javascript 呢?我是否必须有一个与表单/按钮名称一起提交的隐藏字段?
或者最好为页面上的每个按钮使用不同的目标脚本?
What I want is to be able to make my CGI script do different things depending on what action initiated the calling of the script.
For example, if one button is pressed, a database is cleared. If another button is pressed, a form is submitted and that data is added to the database.
Should I be doing something like adding the name of the form/button to the end of the POST
data submitted in jQuery and then .pop
ing it off in the script?
Or is there some other data that's already sent in the POST
that I could get from FieldStorage
that would give me the information I need to decide what the script should do when it's called?
And what if I wasn't using javascript? Would I have to have a hidden field that gets submitted with the name of the form/button?
Or is it best to use a different target script for each button on a page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
POST 请求包含您提交的表单的所有元素。因此,如果您有一个包含多个提交按钮的表单:
那么每次使用“Clear DB”按钮提交表单时,您都会收到一个 POST 请求,其请求数据如下所示:ClearDB=Clear%20DB&hostname=。
基本上,要获取您按下的提交表单的按钮的名称,您只需搜索按钮的名称即可。另一种方法是对按钮使用相同的名称,但使用不同的值:
然后您只需检查获得的请求元素的值(在上面的情况下它将是“action”)。
因此,在 Python 中,您实际上只需要使用 FieldStorage.getfirst() 方法获取通过 FieldStorage 类获得的值。
POST request includes all the elements of the form you submit. So, if you have a form with several submit buttons:
then you would get a POST request with request data looking like: ClearDB=Clear%20DB&hostname= every time you submit the form using the "Clear DB" button.
Basically, to get the name of the button you pressed to submit the form, you just need to search for the button's name. Another approach is using same name for buttons, but different values:
Then you just need to check for the value of the request element you get (in the case above it would be "action").
In Python you would therefore just need to get the value you got via the FieldStorage class indeed, using FieldStorage.getfirst() method.