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("")

Updated on: 09-Sep-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements