Form Input Elements

  • The form tag:
  • In your HTML code, the area containing all the input elements has to be between the <form> tag and the </form> tag.
  • The <form> needs 2 attributes: "method" and "action".  "method" should be either "get" or "post" and action should name the Python program that will be called.
  • <form method="get" action="harry.py">
  • In Python, you can start getting the input values with the statement: form = cgi.FieldStorage()

 

What the user sees HTML code Python code
  <input name="Press_me" type="submit" value="Yo!" /> if form.getvalue("Press_me") == "Yo":
your name please: <input name="its_name" type="text" value="Fred" />

(the value attribute is optional and is the default
if the user doesn't enter anything)
if form.getvalue("its_name","Fred") == "Fred":
send me email:  <input name="subscription" type="checkbox" value="yes"/> if form.getvalue("subscription","") == "yes":
yes     no     huh? <input checked="checked" name="smart" type="radio" value="ayup" />
<input name="smart" type="radio" value="nope" />
<input name="smart" type="radio" value="dunno" />

(These 3 radio buttons all have the same name ("smart") because they form a
family -- only one of them can be on at a time)
if form.getvalue("smart") == "ayup":
# for the smart folk
elif form.getvalue("smart") == "nope":
# etc
My thoughts:  <textarea name="Thoughts"
style="width: 180px; height: 90px"></textarea>
memoirs = form.getvalue("Thoughts","")
Folks:  <select name="guys" size="3" >
<option>Harry</option>
<option>Fred</option>
<option>George</option>
<option>Voldy</option>
</select>

(there are many possibilities for the <select> list:
- you can pre-choose an answer
- you can allow multiple choices
- you can choose that if the user chooses "Harry" the value "hero" should be sent
guy = form.getvalue("guys","")