Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách truyền dữ liệu Checkbox đến Python CGI script?

Truyền dữ liệu Checkbox đến chương trình CGI

Checkbox dùng khi cần chọn nhiều hơn một tùy chọn.

Ví dụ HTML form với hai checkbox:

<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" /> Physics
  <input type="submit" value="Select Subject" />
</form>

Script checkbox.cgi xử lý input:

#!/usr/bin/python
import cgi, cgitb
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 "</html>"