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

更新日期:2020-6-16

877 閱讀數

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.