Lecture 51:

posted May 7, 2013, 6:27 AM by Samuel Konstantinovich   [ updated May 7, 2013, 8:59 AM ]
Do Now:
Write a function getFieldData() that calls cgi.FieldStorage() and returns a dictionary of the key:value pairs. 
This function can be used in later programs.

Example: if your py file was run as:
http://lisa.stuy.edu/~name/file.py?a=3&b=4
getFieldData() returns a dictionary {'a':'3','b':'4'} which would need to be displayed on a web page.

Test it on the web server like this:
#!/usr/bin/python
print 'Content-Type: text/html\n'
def ... YOUR FUNCTION HERE
  ...your function here
print getFieldData()

HTML FORMS:

Make an html file  input.html :
'''<form name="input" action="process.py" method="get"><br>
    First Name: <input type="text" name="first"><br>
    <input type="submit" value="Submit">
    </form>'''

Now make a py file process.py, and have it print the form information using your getFieldData:

#!/usr/bin/python
import cgi
def getFieldData():
    ????PUT YOUR CODE HERE!
    ????
page = 'Content-type: text/html\n\n'
page +='<html><head><title>Test Form</title></head>\n'
page +='<body>'
page+= str(getFieldDict())
page+='</body>'
print page

Homework:
1. Research forms on w3schools or some other tutorial site. Make an HTML page with at least 3 new form types, no more text fields please. Link this form to the py file in part 2.
2. Make a py file that you display an html page. Make the page respond to the different form elements. If you use a checkbox, you should make it print a different results if the box is checked or unchecked. 



Comments