如何将单选按钮数据传递给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>"