Computer >> Máy Tính >  >> Lập trình >> Python

Làm cách nào để chuyển Dữ liệu nút radio sang tập lệnh Python CGI?

Chuyển Dữ liệu Nút Radio sang Chương trình CGI

Các nút Radio được sử dụng khi chỉ cần chọn một tùy chọn.

Đây là mã HTML mẫu cho biểu mẫu có hai nút radio -

<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>

Kết quả của mã này là dạng sau -

 Maths  Physics Select Subject

Dưới đây là tập lệnh radiobutton.py để xử lý đầu vào do trình duyệt web cung cấp cho nút radio -

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