Passing Drop Down Box Data to CGI Program in Python

PythonServer Side ProgrammingProgramming

Drop Down Box is used when we have many options available but only one or two will be selected.


Here is example HTML code for a form with one drop down box −

<form action = "/cgi-bin/" method = "post" target = "_blank">
<select name = "dropdown">
<option value = "Maths" selected>Maths</option>
<option value = "Physics">Physics</option>
<input type = "submit" value = "Submit"/>


The result of this code is the following form −

Below is script to handle input given by web browser.

# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
if form.getvalue('dropdown'):
   subject = form.getvalue('dropdown')
   subject = "Not entered"
print "Content-type:text/html\r
" print "<html>" print "<head>" print "<title>Dropdown Box - Sixth CGI Program</title>" print "</head>" print "<body>" print "<h2> Selected Subject is %s</h2>" % subject print "</body>" print "</html>"
Updated on 31-Jan-2020 08:06:27