如何使用 Python CGI 指令碼來處理簡單表單資料?
假設有一個 HTML 檔案,如下所示 −
<form action="getData.py" method="post"> FirstName: <input type="text" name="first_name"> LastName: <input type="text" name="last_name"> <input type="submit" value="go"> </form>
提交此表單後,它應轉到名為 "getData.py" 的 Python 頁面,您應從此 HTML 頁面 獲取資料並顯示。以下是 Python CGI 的程式碼。
#!C:\Python27\python.exe # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields first_name = form.getvalue('first_name') last_name = form.getvalue('last_name') print("Content-type:text/html") print print("") print("") print("Hello - Second CGI Program") print("") print("") print(" Hello %s %s " % (first_name, last_name)) print("") print("")
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP