Passing Checkbox Data to CGI Program in Python

Checkboxes are used when more than one option is required to be selected.

Example

Here is example HTML code for a form with two checkboxes −


Maths Physics

Output

The result of this code is the following form −

Below is checkbox.cgi script to handle input given by web browser for checkbox button.

#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
if form.getvalue('maths'):
   math_flag = "ON"
else:
   math_flag = "OFF"
if form.getvalue('physics'):
   physics_flag = "ON"
else:
   physics_flag = "OFF"
print "Content-type:text/html\r\n\r\n"
print ""
print ""
print "Checkbox - Third CGI Program"
print ""
print ""
print "

CheckBox Maths is : %s

" % math_flag print "

CheckBox Physics is : %s

" % physics_flag print "" print ""
Updated on: 2020-01-31T08:02:13+05:30

677 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements