Computer >> Computer tutorials >  >> Programming >> Python

How to process a simple form data using Python CGI script?


suppose there is an HTML file as below −

<form action="getData.py" method="post">
   FirstName: <input type="text" name="first_name">
   LastName: <input type="text" name="last_name">
   <input type="submit" value="go">
</form>

after submitting this form it should go to a python page named getData.py, where you should fetch the data from this HTML page and show. then below is the code for python CGI

#!C:\Python27\python.exe
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')
print("Content-type:text/html")
print
print("")
print("")
print("Hello - Second CGI Program")
print("")
print("")
print("
   Hello %s %s
   " % (first_name, last_name))
print("")
print("")