SLIDE 4 4
The Big Example Part 1 (the form)
(standard header stuff…) <body> <h1> Welcome to The Ultimate Survey </h1> <form method="post" action="set9_survey.py"> <p> Favorite food: <input type="text" name="food" /> </p> <p> Favorite color: <label><input type="radio" name="color" value="red"/> Red </label> <label><input type="radio" name="color" value="gold"/> Gold </label> <label><input type="radio" name="color" value="blue"/> Blue </label> </p> <p><input type="submit" value="Submit" /> </p> </form> </body> </html>
survey.html
The Big Example Part 2 (CGI to receive)
#!/usr/bin/env python3 import cgi import cgitb; cgitb.enable() form = cgi.FieldStorage() # instantiate only once! # get inputs from browser food = form.getvalue(“food”) color = form.getvalue(“color”) # Save result in file. Use colon as separator
- utfile = open ("favorites.txt","a")
- utfile.write(food + " : " + color + "\n")
- utfile.close()
#generate output as feedback for the user print ("Content-Type: text/html\n") print ('''\ <!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <title>Survey Feedback</title> </head> <body> <h1>Thank you for filling out our survey</h1> <p>Your responses have been recorded as follows:</p> <ul> ''') print (“<li>Favorite food: “ + food + “</li>”) print (“<li>Favorite color: “ + color + “</li>”) print (“</ul></body></html>”)
survey.py