如何将单选按钮数据传递到 Python CGI 脚本?
将单选按钮数据传递到 CGI 程序
仅需要选择一个选项时,请使用单选按钮。
以下是带有两个单选按钮的表单示例 HTML 代码 −
<form action = "/cgi-bin/radiobutton.py" 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" /> </form>
此代码的结果为以下表单 −
Maths Physics Select Subject
以下是 radiobutton.py 脚本,用于处理 Web 浏览器针对单选按钮提供的输入 −
#!/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('subject'): subject = form.getvalue('subject') else: 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>"
广告