如何將文字區域資料傳遞給 Python CGI 指令碼?
將文字區域資料傳遞給 CGI 程式
當需要將多行文字傳遞給 CGI 程式時,便會使用 TEXTAREA 元素。
以下是一個包含 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>"
廣告