<HTML>
<HEAD>
<TITLE>
Hello World</TITLE>
</HEAD>
<BODY>
<H1>Greetings</H1>
</BODY>
</HTML>
as a file in your www directory on ella. Look at it through a browser.
1.2) Save some simple cgi code such as
#!/usr/bin/env python
import cgi
print "Content-Type: text/html\n"
print """
<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
</HEAD>
<BODY>
<H1>Greetings</H1>
</BODY>
</HTML>
"""
in your cgi directory. Run it on the command-line to check the syntax.
Change file permissions to executeable ("chmod 700 filename"
or "chmod u+x filename").
Look at it through your browser.
browser | server
user requests | html document
| server finds HTML file | and sends page back |
CGI:
browser | server
user requests | a form
| server finds the HTML form | and sends it back to user user fills out form |
| CGI application executes | program and sends results back to user |
<form action="http://ella.slis.indiana.edu/~username/cgi/example"
method="post">
<input type="radio" name="drink" value="tea" checked >
Tea <br>
<input type="submit" value="Place order">
<input type="radio" name="drink" value="coffee" > Coffee <br>
<input type="radio" name="drink" value="hot chocolate" > Hot Chocolate
<p>
</form>
This is the source code of a cgi file that processes the form:
#!/usr/bin/env python
#
######### don't change the following three lines: ###########
import cgi
print "Content-Type: text/html\n"
form = cgi.FieldStorage()
## add a form.getvalue for each of the names in your form: ##
drink = form.getvalue("drink")
########## start of HTML code ###########
print """
<html>
<head>
<title>What would you like to drink</title>
</head>
<body>
<h4>Your drink: </h4><p>
"""
############ end of HTML code #############
if drink == "tea":
print "You requested tea."
elif drink == "coffee":
print "You requested coffee."
elif drink == "hot chocolate":
print "You requested hot chocolate."
else:
print "You need to select a drink!"
########### start of HTML code ###########
print """
<p>Thank you for your visit. Please come again. <p>
</body></html>
"""
############# end of HTML code ##############
3.2) Save the cgi file. Run it on the command line to check for
syntax errors.
3.3) Change the form method (in the html file) to "get". Reload the form in your browser. Then submit the form. Can you notice a difference in the address field of the browser?
3.4) Add a checkbox to the form (such as "Do you want milk? Yes/No") and a text area where customers can type in what kind of cake they would like to order. Change your cgi script so that it includes these in its reply, such as "you requested tea with milk", "sorry we are out of chocolate cake". The checkbox and text area must have distinct names in the form. You need a line with form.getvalue() in your cgi file for each name in your html form.
4.2) Write an HTML file that does not contain a form but contains three links that send URLs with attached parameters "tea", "coffee", "hot chocolate" to the cgi file. (In this manner you can write cgi files that communicate with other cgi files on the web.)
4.3) (optional) Choose a search engine on the web and analyse how they attach the search paramaters to the URL. Write a form that sends data to that search engine.
#!/usr/bin/env python
#
######### don't change the following six lines: ###########
import cgi
print "Content-Type: text/html\n"
form = cgi.FieldStorage()
import sys
import traceback
sys.stderr = sys.stdout
## add a form.getvalue for each of the names in your form: ##
drink = form.getvalue("drink")
########## start of HTML code ###########
print """
<html>
<head> <title>What would you like to drink?
</title> </head>
<body>
<h4>Your drink: </h4><p>
"""
########## end of HTML code ###########
try:
if drink == "tea":
print "You requested tea."
elif drink == "coffee":
print "You requested coffee."
elif drink == "hot chocolate":
print "You requested hot chocolate."
else:
print "You must choose a drink"
except:
print "\n\n<PRE>"
traceback.print_exc()
print "</PRE>"
########## start of HTML code ###########
print """
<p>Thank you for your visit. Please come again. <p>
</body></html>
"""
########## end of HTML code ###########
Change 'elif drink == "hot chocolate":' to 'elif drink1 == "hot chocolate":' and see what happens.