开发者

Finding out event that called a CGI script

开发者 https://www.devze.com 2022-12-31 12:35 出处:网络
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.

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开发者_StackOverflow中文版 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消