Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

확인란 데이터를 Python CGI 스크립트에 전달하는 방법은 무엇입니까?

<시간/>

CGI 프로그램에 체크박스 데이터 전달

체크박스는 둘 이상의 옵션을 선택해야 할 때 사용됩니다.

다음은 두 개의 확인란이 있는 양식에 대한 예제 HTML 코드입니다. -

<form action = "/cgi-bin/checkbox.cgi" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" value = "on" /> Maths
<input type = "checkbox" name = "physics" value = "on" /> Physics
<input type = "submit" value = "Select Subject" />
</form>

이 코드의 결과는 다음 형식입니다. -

Maths  Physics Select Subject

다음은 체크박스 버튼에 대해 웹 브라우저에서 제공한 입력을 처리하는 체크박스.cgi 스크립트입니다.

#!/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('maths'):
   math_flag = "ON"
else:
   math_flag = "OFF"
if form.getvalue('physics'):
   physics_flag = "ON"
else:
   physics_flag = "OFF"
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Checkbox - Third CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> CheckBox Maths is : %s</h2>" % math_flag
print "<h2> CheckBox Physics is : %s</h2>" % physics_flag
print "</body>"
print "</html>"