Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Process 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: 2023-09-09T23:02:23+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements