如何将下拉框数据传递给 Python CGI 脚本?
将下拉框数据传递给 CGI 程序
当有多个选项可用但只选择一或两个选项时,将使用下拉框。
下面是一个带有一个下拉框的表单的 HTML 代码示例:
<form action = "/cgi-bin/dropdown.py" method = "post" target = "_blank"> <select name = "dropdown"> <option value = "Maths" selected>Maths</option> <option value = "Physics">Physics</option> </select> <input type = "submit" value = "Submit"/> </form>
此代码的结果是以下表单:
Submit
下面是 dropdown.py 脚本,用于处理网络浏览器提供的输入。
#!/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('dropdown'): subject = form.getvalue('dropdown') else: subject = "Not entered" print "Content-type:text/html\r\n\r\n" 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>"
广告