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

จะส่งผ่าน Checkbox Data ไปยังสคริปต์ Python CGI ได้อย่างไร


การส่งข้อมูลช่องทำเครื่องหมายไปยังโปรแกรม 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" /> Physics
<input type = "submit" value = "Select Subject" />
</form>

ผลลัพธ์ของรหัสนี้คือรูปแบบต่อไปนี้ -

Maths  Physics Select Subject

ด้านล่างคือสคริปต์ checkbox.cgi สำหรับจัดการอินพุตที่เว็บเบราว์เซอร์กำหนดสำหรับปุ่มช่องทำเครื่องหมาย

#!/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 "<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>"