Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีส่งข้อมูล Drop Down Box ให้ Python CGI Script

การส่งข้อมูล Drop Down Box ให้ CGI Program

Drop Down Box ใช้เมื่อมีหลายตัวเลือกแต่จะเลือกเพียงหนึ่งหรือสองตัวเลือก

ตัวอย่าง HTML Code สำหรับ Form ที่มี 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 สำหรับจัดการ Input จาก Web Browser

#!/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('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>"