Python on the web: How to get a CGI script to recognize two submit buttons with separate values in one form

In one of my Python cgi scripts I have a web form with some hidden values, text fields, and two submit buttons. I want my cgi script to do something different depending on which submit button was clicked.

One of the submit buttons is for completing a review without making any changes and is called “Submit No Changes”. The other submit button is for the case where the reviewer made changes in the form, and is called “Capture Edits”. Both of these submit buttons are in the same HTML web form and point to the same cgi script.

At first I set up my submit buttons like this:

<input type="submit" name="capture_edits" value="Capture Edits">
<input type="submit" name="submit_no_changes" value="Submit No Changes">

but I noticed that neither the names or values from these buttons were being captured by cgi.FieldStorage() . After sending a POST to the cgi script by clicking the “Capture Edits” submit button, the name “capture_edits” wasn’t being captured in the list of form keys when I printed them like this:

import cgi
form = cgi.FieldStorage()

print "Content-type: text/html"
print ""
print form.keys()

It turns out, the name-value pairs from the submit button that was clicked will only be captured by cgi.FieldStorage() when type="submit" is listed to the right of the name and value. For example, this worked:

<input name="capture_edits" value="Capture Edits" type="submit">
<input name="submit_no_changes" value="Submit No Changes" type="submit">

Perhaps this is because the browser doesn’t pay attention to anything after it sees the key word “submit” when a form POST is submitted?

Leave a Reply