How to pass Drop Down Box Data to Python CGI script?

Drop down boxes are useful HTML form elements when you have multiple options but only want to allow selection of one or two items. Here's how to pass dropdown data to a Python CGI script and process it server-side.

HTML Form with Dropdown

First, create an HTML form with a dropdown (select) element ?

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

This creates a dropdown with subject options. The name="dropdown" attribute is crucial as it identifies the field in the CGI script.

Python CGI Script

Here's the complete dropdown.py script to handle the form submission ?

#!/usr/bin/python3

# Import modules for CGI handling
import cgi
import cgitb

# Enable CGI error reporting
cgitb.enable()

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from dropdown field
if form.getvalue('dropdown'):
    subject = form.getvalue('dropdown')
else:
    subject = "Not entered"

# Send HTTP headers
print("Content-type:text/html\r\n\r\n")

# Generate HTML response
print("<html>")
print("<head>")
print("<title>Dropdown Box - CGI Program</title>")
print("</head>")
print("<body>")
print("<h2>Selected Subject is %s</h2>" % subject)
print("</body>")
print("</html>")

How It Works

The CGI script processes the dropdown data in these steps:

  1. Form Parsing: cgi.FieldStorage() parses the submitted form data
  2. Data Extraction: form.getvalue('dropdown') retrieves the selected value
  3. Validation: Check if a value was selected to handle empty submissions
  4. Response Generation: Output proper HTTP headers and HTML content

Enhanced Example with Multiple Dropdowns

You can handle multiple dropdowns in a single form ?

#!/usr/bin/python3

import cgi
import cgitb

cgitb.enable()
form = cgi.FieldStorage()

# Handle multiple dropdowns
subject = form.getvalue('subject') or "Not selected"
level = form.getvalue('level') or "Not selected"

print("Content-type:text/html\r\n\r\n")
print("<html><body>")
print("<h2>Form Results</h2>")
print("<p>Subject: %s</p>" % subject)
print("<p>Level: %s</p>" % level)
print("</body></html>")

Key Points

  • Always include the shebang #!/usr/bin/python3 at the top
  • Enable cgitb.enable() for debugging CGI errors
  • Send proper HTTP headers before any HTML output
  • Use form.getvalue() to safely retrieve form data
  • Handle cases where no option is selected

Conclusion

Processing dropdown data in CGI involves creating an HTML form with a select element and using Python's cgi module to parse the submitted values. Always validate the input and send proper HTTP headers for reliable form handling.

---
Updated on: 2026-03-24T20:03:47+05:30

624 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements