How to pass Checkbox Data to Python CGI script?

PythonServer Side ProgrammingProgramming

Passing Checkbox Data to CGI Program

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

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

<form action = "/cgi-bin/checkbox.cgi" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" value = "on" /> Maths
<input type = "checkbox" name = "physics" value = "on" /> Physics
<input type = "submit" value = "Select Subject" />

The result of this code is the following form −

Maths  Physics Select Subject

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

# 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"
   math_flag = "OFF"
if form.getvalue('physics'):
   physics_flag = "ON"
   physics_flag = "OFF"
print "Content-type:text/html\r
" print "<html>" print "<head>" print "<title>Checkbox - Third CGI Program</title>" print "</head>" print "<body>" print "<h2> CheckBox Maths is : %s</h2>" % math_flag print "<h2> CheckBox Physics is : %s</h2>" % physics_flag print "</body>" print "</html>"
Updated on 16-Jun-2020 12:17:52