如何將單選按鈕資料傳遞到 Python CGI 指令碼?
將單選按鈕資料傳遞到 CGI 程式
僅需要選擇一個選項時,請使用單選按鈕。
以下是帶有兩個單選按鈕的表單示例 HTML 程式碼 −
<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>
此程式碼的結果為以下表單 −
Maths Physics Select Subject
以下是 radiobutton.py 指令碼,用於處理 Web 瀏覽器針對單選按鈕提供的輸入 −
#!/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>"
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP