I have a basic html form which I want to use with two submit buttons. The CGI script takes the form value and processes it (regardless of which submit button used), but I want each button to be associated with different actions later in the script, i.e. print 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.
<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 pressed.
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."
because form acts like a Python dict
and you can check if a given name is in it. It should include the names of all form entities sent, so, with one field and two submit buttons, it should contain the name of the form field and the name of the submit button that was pressed.