Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách truyền dữ liệu Drop Down Box đến Python CGI script?

Truyền dữ liệu Drop Down Box đến chương trình CGI

Drop Down Box dùng khi có nhiều lựa chọn nhưng chỉ chọn một hoặc hai.

Ví dụ HTML form có một drop down box:

<form action="/cgi-bin/dropdown.py" method="post" target="_blank">
  <select name="dropdown">
    <option value="Maths" selected>Maths</option>
    <option value="Physics">Physics</option>
  </select>
  <input type="submit" value="Submit"/>
</form>

Script dropdown.py xử lý input từ trình duyệt:

#!/usr/bin/python
import cgi, cgitb
form = cgi.FieldStorage()
if form.getvalue('dropdown'):
    subject = form.getvalue('dropdown')
else:
    subject = "Not entered"
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Dropdown Box - Sixth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"