CS1116/CS5018

Web Development 2

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Big head

Two files

The user requests a form, fills it in, submits the data, which runs a program, which returns the result of a calculation

Self-processing page

Self-processing page

The user requests a program, which may either output a form or the
            result of a calculation, depending on whether the user has supplied data or not

Pseudocode

if the user has not sent us any data:
    output a web page containing the form
else: 
    calculate the person's hat size
    output a web page containing the form and the hat size

Or:

set the hat size to 0
if the user has sent us some data:
    calculate the person's hat size
output a web page containing the form and the hat size

head_to_hat.py: a self-processing page

Complete the program:

#!/usr/local/bin/python3

from cgitb import enable 
enable()

from cgi import FieldStorage
from math import pi

print('Content-Type: text/html')
print()

form_data = FieldStorage()
hat_size = 0.0
if 



print("""
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Big head</title>
        </head>
        <body>
            <form action="head_to_hat.py" method="get">
                <label for="circumference">Circumference: </label>
                <input type="text" name="circumference" id="circumference" />
                <label for="hat_size">Hat size: </label>
                <input type="text" name="hat_size" id="hat_size" value = "%f" disabled />
                <input type="submit" />
            </form>
        </body>
    </html>""" % (hat_size))

Sticky forms

Private study

Modify head_to_hat.py so that it also validates the user's input