如何將文字框資料傳遞給 Python CGI 指令碼?


將文字框資料傳遞給 CGI 程式

TEXTAREA 元素用於將多行文字傳遞給 CGI 程式。

以下是帶有 TEXTAREA 框的表單的 HTML 程式碼示例 -

<form action = "/cgi-bin/textarea.py" method = "post" target = "_blank">
<textarea name = "textcontent" cols = "40" rows = "4">
Type your text here...
</textarea>
<input type = "submit" value = "Submit" />
</form>

此程式碼的結果是以下表單 -

Type your text here...
 Submit

以下是 textarea.cgi 指令碼,用於處理 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('textcontent'):
   text_content = form.getvalue('textcontent')
else:
   text_content = "Not entered"
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>";
print "<title>Text Area - Fifth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Entered Text Content is %s</h2>" % text_content
print "</body>"

更新於:16-Jun-2020

684 次瀏覽

啟動您的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.