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

Truyền dữ liệu Radio Button đến chương trình CGI trong Python


Radio Buttons (Nút radio) được sử dụng khi chỉ cần chọn một tùy chọn.

Ví dụ

Dưới đây là mã HTML ví dụ cho biểu mẫu với 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à biểu mẫu sau:

Truyền dữ liệu Radio Button đến chương trình CGI trong Python

Dưới đây là script radiobutton.py để xử lý đầu vào từ trình duyệt web cho nút radio:

#!/usr/bin/python
# Nhập mô-đun để xử lý CGI
import cgi, cgitb

# Tạo thể hiện FieldStorage
form = cgi.FieldStorage()

# Lấy dữ liệu từ trường
if form.getvalue('subject'):
    subject = form.getvalue('subject')
else:
    subject = "Not set"

print "Content-type:text/html\r\n\r\n"
print ""
print ""
print "Radio - Fourth CGI Program"
print ""
print ""
print "

Selected Subject is %s

" % subject print "" print ""