在 Python 中將單選按鈕資料傳遞給 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>

此程式碼的結果如下所示 −

以下是 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>"

更新於: 2020-01-31

255 次瀏覽

事業起航

完成課程,獲得認證

開始
廣告
© . All rights reserved.