httpd
.
Place the following file in a convenient spot in the WWW server's
directory tree. E.g., if you place it in the subdirectory
<TITLE>Test Form Input</TITLE>
<H1>Test Form Input</H1>
<FORM METHOD="POST" ACTION="/cgi-bin/test.py">
<INPUT NAME=Name> (Name)<br>
<INPUT NAME=Address> (Address)<br>
<INPUT TYPE=SUBMIT>
</FORM>
Selecting this file's URL from a forms-capable browser such as Mosaic
or Netscape will bring up a simple form with two text input fields and
a ``submit'' button.
But wait. Before pressing ``submit'', a script that responds to the
form must also be installed. The test file as shown assumes that the
script is called cgi-bin
directory. Here's the test script:
#!/usr/local/bin/python
import cgi
print "Content-type: text/html"
print # End of headers!
print "<TITLE>Test Form Output</TITLE>"
print "<H1>Test Form Output</H1>"
form = cgi.SvFormContentDict() # Load the form
name = addr = None # Default: no name and address
# Extract name and address from the form, if given
if form.has_key('Name'):
name = form['Name']
if form.has_key('Address'):
addr = form['Address']
# Print an unnumbered list of the name and address, if present
print "<UL>"
if name is not None:
print "<LI>Name:", cgi.escape(name)
if addr is not None:
print "<LI>Address:", cgi.escape(addr)
print "</UL>"
The script should be made executable (`chmod +x script').
If the Python interpreter is not located at
Now that everything is installed correctly, we can try out the form. Bring up the test form in your WWW browser, fill in a name and address in the form, and press the ``submit'' button. The script should now run and its output is sent back to your browser. This should roughly look as follows:
Test Form Output