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.
yes, multiple submit buttons can include in the html form. One simple example is given below.
Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.
A form consists of two distinct parts: the HTML code and the CGI program. HTML tags create the visual representation of the form, while the CGI program decodes (or processes) the information contained within the form.
Advertisements. The Common Gateway Interface, or CGI, is a set of standards that define how information is exchanged between the web server and a custom script. The CGI specs are currently maintained by the NCSA.
<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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With