Passing Radio Button Data to CGI Program in Python

PythonServer Side ProgrammingProgramming

Radio Buttons are used when only one option is required to be selected.


Here is example HTML code for a form with two radio buttons −

<form action = "/cgi-bin/" method = "post" target = "_blank">
<input type = "radio" name = "subject" value = "maths" /> Maths
<input type = "radio" name = "subject" value = "physics" /> Physics
<input type = "submit" value = "Select Subject" />

The result of this code is the following form −

Below is script to handle input given by web browser for radio button −

# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
if form.getvalue('subject'):
   subject = form.getvalue('subject')
   subject = "Not set"
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Radio - Fourth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"
Published on 31-Jan-2020 12:33:20