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

更新日期:2020-6-16

877 阅读数

开启你的职业生涯

完成课程,获得认证

开始
广告