如何将下拉框数据传递给 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>"
广告