Anatomy of a Forms-processing Python program

A Python program that displays all transmitted input form elements

#! /usr/bin/python #[1]

import cgi  #[2]

import cgitb  #[3]
cgitb.enable() #[4]

print ('Content-type: text/html\n')  #[5]

[6]
Top_HTML = '''
<html>
<head>
<title>Sample Python Forms-responder</title>
</head>
<body>
<b>The Transmitted HTML input elements are...</b>
<p>
'''

Bottom_HTML = '</body></html>'

def ShowInputElements():
    elements = cgi.FieldStorage()  #[7]

    print (Top_HTML)
    keys = elements.keys()  #[8]
    for akey in keys:
        print (akey + ' = ' + str(elements.getvalue(akey)) + '<br/>')  #[9]
    print (Bottom_HTML)

ShowInputElements()  #[10]

[1] Nicknamed the "shebang", this line tells UNIX-based web servers where it can find the Python interpreter to execute the code below.  Make sure that this location is the correct place to go. On Windows, this has no effect, but is not harmful, and is useful to leave in the file if you're developing code that needs to run on both systems.

[2] The cgi library (CGI = "Common Gateway Interface") will obtain all of the transmitted input elements from the web server.

[3] cgitb is a library whose single function, enable(), you'll want to execute.  In case your Python code crashes with a runtime error (alas, all too frequently for mere mortals), cgitb code will display a somewhat reasonable error message in your browser, enabling you to find the source of the bug.

[4] Remember to execute the enable() function as soon as your code is read.

[5]  You are sending HTML to the client's browser, and must indicate that by outputting this line and then a blank line.

[6]  Since the starting HTML is pretty much boiler-plate code, we'll put all of it into a variable Top_HTML (note that we need 3 single-quotes to start a multi-line string).

[7]  We call cgi-FieldStorage() which will gather, into a dictionary-like data structure, all of the elements that the user filled out and whose browser transmitted this to the web server.

[8]  By getting a list of all of the dictionary keys, we'll be able to step through each one, and call for its value.

[9]  Here we print the key (which is the name given to the form element, e.g. in the following the name is "fred"):
         <input type="text" name="fred" size="20">
and its value, namely what the user printed or chose.

Important Note: If the user does not fill in a form element, or check a checkbox, it may not be transmitted to the web server, so it is important that you check for the existence of a form element in the dictionary before you call for it's value.  As in:

if 'CheckWhatever' in form:
    print ('The checkbox was checked by the user')
else:
    print ('The checkbox was not checked.')

if 'FirstName' in form:
    print ("The user's first name is: " + form['FirstName'].value)
else:
    print )'The lazy user never filled in his/her first name.')

[10]  Be sure to call the function so that when this file is read, the last command actually executes the function.