CS1116/CS5018
Web Development 2
Dr Derek Bridge
School of Computer Science & Information Technology
University College Cork
colour.html
: a page with a form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mint tint</title>
</head>
<body>
<form action="colour.py" method="get">
<p>What's Derek's favourite colour?</p>
<input type="radio" name="guess" value="red" id="red" checked />
<label for="red">Red</label>
<input type="radio" name="guess" value="green" id="green" />
<label for="green">Green</label>
<input type="radio" name="guess" value="blue" id="blue" />
<label for="blue">Blue</label>
<input type="submit" value="Hit me!" />
</form>
</body>
</html>
colour.py
: an incomplete program
#!/usr/local/bin/python3
from cgitb import enable
enable()
from cgi import FieldStorage
print('Content-Type: text/html')
print()
dereks_mint_tint = 'green';
form_data = FieldStorage()
print("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mint tint</title>
</head>
<body>
<p>
You are %s. Derek's favourite colour is %s.
</p>
</body>
</html>""" % (outcome, dereks_mint_tint))
toppings.html
: a page with a form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Top toppings</title>
</head>
<body>
<form action="toppings.py" method="get">
<p>What are Derek's favourite pizza toppings?</p>
<input type="checkbox" name="guesses" value="anchovies" id="anchovies" />
<label for="anchovies">Anchovies</label>
<input type="checkbox" name="guesses" value="chocolate" id="chocolate" />
<label for="chocolate">Chocolate</label>
<input type="checkbox" name="guesses" value="nails" id="nails" />
<label for="nails">Finger nails</label>
<input type="checkbox" name="guesses" value="olives" id="olives" />
<label for="olives">Olives</label>
<input type="checkbox" name="guesses" value="pepperoni" id="pepperoni" />
<label for="pepperoni">Pepperoni</label>
<input type="submit" value="Dish it up!" />
</form>
</body>
</html>
toppings.py
: an incomplete program
#!/usr/local/bin/python3
from cgitb import enable
enable()
from cgi import FieldStorage
print('Content-Type: text/html')
print()
dereks_top_toppings = ['anchovies', 'chocolate', 'pepperoni']
form_data = FieldStorage()
print("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Top toppings</title>
</head>
<body>
<p>
%s
</p>
</body>
</html>""" % (outcome))
Summary
-
Where you are expecting a
name
to be paired with a single value,
-
e.g.
<input type="text">
, <input type="radio">
, …
- then use
getfirst
- Q: Can the user send us no data? Can s/he supply a value other than red, green or blue?
Can s/he supply more than one value? How? What should we do?
Summary
-
Where you are expecting a
name
to be paired with zero, one or more values,
- e.g.
<input type="checkbox">
- then use
getlist
- Q: What happens if the user sends us no data? What if s/he supplies just one value?
Do we need to do anything special?
- Q: What happens if the user supplies values other than anchovies, chocolate, nails, olives and pepperoni?
How can s/he do that? What should we do?