Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Passing Checkbox Data to CGI Program ใน Python

Checkboxes ใช้เมื่อต้องการให้เลือกมากกว่าหนึ่งตัวเลือก

ตัวอย่างโค้ด HTML

โค้ด HTML สำหรับฟอร์มที่มี checkboxes สองตัว:

Maths Physics

ผลลัพธ์

ผลลัพธ์คือฟอร์มดังภาพ:

Passing Checkbox Data to CGI Program ใน Python

ด้านล่างคือสคริปต์ checkbox.cgi สำหรับจัดการข้อมูลจากเว็บเบราว์เซอร์สำหรับ checkbox button:

#!/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('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 ""
print ""
print "Checkbox - Third CGI Program"
print ""
print ""
print "

CheckBox Maths is : %s

" % math_flag print "

CheckBox Physics is : %s

" % physics_flag print "" print ""