如何將單選按鈕資料傳遞到 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>"

更新日期: 16-6-2020

876 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.