Posting HTML form data to Python by FieldStorage()
Display one HTML form with input elements and on submit we will collect data in Python script. The form will submit by POST method.
Here is the HTML form
The action attribute of above form is submitting to form-submit.py.
We will collect the above form data by using FieldStorage()
The code inside form-submit.py is here.
Above code will display all the data submitted through the form. Here if any data is not filled then above code will generate error. We will use try catch error handling to manage the blank data.
print("HTTP/1.0 200 OK\n")
import cgi
form = cgi.FieldStorage()
try:
f_name=form["f_name"].value
except :
f_name=' First name is blank '
try:
s_name=form["s_name"].value
except :
s_name=' Second name is blank '
try:
r1=form["r1"].value
except :
r1=' No selection of Sex'
try:
my_class=form["class"].value
except:
my_class= ' Select Class '
print("<br><b>First Name</b>",f_name)
print("<br><b>Second Name</b>",s_name)
print("<br><b>Sex</b>",r1)
print("<br><b>Class</b>",my_class)
print("<br><br><br><a href=form.htm>Back to Form</a>")
Output is here
First Name First name
Second Name Second name
Sex male
Class Five