找出调用 CGI 脚本的事件

发布于 2024-09-02 14:20:39 字数 385 浏览 0 评论 0原文

我想要的是能够让我的 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 .poping 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2024-09-09 14:20:39

POST 请求包含您提交的表单的所有元素。因此,如果您有一个包含多个提交按钮的表单:

   <form id="mytestform" target="/cgi-bin/script.py" method="POST">
     <input type="submit" name="ClearDB" value="Clear DB"/>
     <input type="submit" name="TestDB" value="Test DB"/>
     <input type="text" name="hostname" />
   </form>

那么每次使用“Clear DB”按钮提交表单时,您都会收到一个 POST 请求,其请求数据如下所示:ClearDB=Clear%20DB&hostname=。

基本上,要获取您按下的提交表单的按钮的名称,您只需搜索按钮的名称即可。另一种方法是对按钮使用相同的名称,但使用不同的值:

<form id="mytestform" target="/cgi-bin/script.py" method="POST">
    <input type="submit" name="action" value="Clear"/>
    <input type="submit" name="action" value="Test"/>
</form>

然后您只需检查获得的请求元素的值(在上面的情况下它将是“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:

   <form id="mytestform" target="/cgi-bin/script.py" method="POST">
     <input type="submit" name="ClearDB" value="Clear DB"/>
     <input type="submit" name="TestDB" value="Test DB"/>
     <input type="text" name="hostname" />
   </form>

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:

<form id="mytestform" target="/cgi-bin/script.py" method="POST">
    <input type="submit" name="action" value="Clear"/>
    <input type="submit" name="action" value="Test"/>
</form>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文