在 Python 中将单选按钮数据传递给 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>

此代码的结果如下所示 −

以下是 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>"

更新于: 2020-01-31

255 次浏览

事业起航

完成课程,获得认证

开始
广告
© . All rights reserved.