如何将复选框数据传递给 Python CGI 脚本?\

pythonserver side programmingprogramming

将复选框数据传递给 CGI 程序

当需要选择多个选项时,使用复选框。

以下是带有两个复选框的表单的示例 HTML 代码 −

<form action = "/cgi-bin/checkbox.cgi" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" value = "on" /> Maths
<input type = "checkbox" name = "physics" value = "on" />物理
<input type = "submit" value = "Select Subject" />
</form>

此代码的结果为以下形式 −

Maths  Physics Select Subject

以下是 checkbox.cgi 脚本,用于处理 Web 浏览器为复选框按钮提供的输入。

#!/usr/bin/python
# 导入模块以进行 CGI 处理
import cgi, cgitb
# 创建 FieldStorage 实例
form = cgi.FieldStorage()
# 从字段获取数据
if form.getvalue('maths'):
   math_flag = "ON"
else:
   math_flag = "OFF"
if form.getvalue('physics'):
   physics_flag = "ON"
else:
   physics_flag = "OFF"
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Checkbox - Third CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> CheckBox Maths is : %s</h2>" % math_flag
print "<h2> CheckBox Physics is : %s</h2>" % physics_flag
print "</body>"
print "</html>"

相关文章


有用资源