Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python CGI Script: Basic HTML Form With Two Submit Buttons

Tags:

python

html

cgi

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.

like image 457
user725236 Avatar asked Jul 25 '11 11:07

user725236


People also ask

Can a HTML form have 2 submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How do I have two submit buttons?

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.

What is forms and CGI?

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.

What is CGI script in Python?

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.


1 Answers

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

like image 123
agf Avatar answered Sep 22 '22 03:09

agf