Python CGI Script: basic HTML form with two submit buttons

I have a basic html form that I want to use with two submit buttons. The CGI script takes the form value and processes it (regardless of the submit button used), but I want each button to be associated with different actions later in the script, i.e. print a different output:

##  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.

+5
source share
1 answer
<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 clicked.

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."

Python dict, , . , , .

+5

All Articles