如何将单选按钮数据传递给 Python CGI 脚本?
pythonserver side programmingprogramming
将单选按钮数据传递给 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>
The result of this code is the following form −
Maths Physics Select Subject
以下是 radiobutton.py 脚本,用于处理 Web 浏览器为单选按钮 − 提供的输入
#!/usr/bin/python
# 导入模块以进行 CGI 处理
import cgi, cgitb
# 创建 FieldStorage 实例
form = cgi.FieldStorage()
# 从字段获取数据
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>"
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

