[Tutor] How can I code to get more than ONE value from checkbox?

Ms Soo Chong s4046441 at student.uq.edu.au
Fri Oct 1 07:38:49 CEST 2004


I'm trying to code a python script (with HTML) to get values from a
html form that consists of about 10 checkbox and a textbox where user
have to key in a value to perform a search.

>From Lee Harr and Danny, I learned that I have to use the following method:

qtype = data.getfirst('qtype')       ---- checkbox name
qtext = data.getfirst('qtext', '')   ---- textbox name

but I don't really know how to do it. Should I use qtype =
data.getlist('qtype') to get the value from the checkbox and how to?

Currently, my method required me to type in every possible combination
of the checkbox and I know that this is wrong. I wanna to include
%(qtype) in my query statement so that the script will look for the
value that contain qtype everytime a form is processs but the way I'm
doing now is wrong.

query = "select %(qtype)s from shot_descriptions where
shot_number=%(qtext)s", qtype, qtext

Thus, can someone help me on this.

Thank you for any help.


-------------- next part --------------
#!/usr/bin/env python
#Created on: 30/09/04
#Help from Lee Harr and Danny Yoo - Python Tutor

import sys, os
import cgi
import cgitb; cgitb.enable()
import pg

def form():
    print """<form method="post" action="">
             <input type=checkbox name="qtype" value="all" checked />
             All<br />
             <input type=checkbox name="qtype" value="project" />
             Project<br />
             <input type=checkbox name="qtype" value="date_string" />
             Date<br />
             <input type=checkbox name="qtype" value="blame" />
             Blame<br />
             <input type=checkbox name="qtype" value="notes" />
             Notes<br /></p>

             <p>Search by shot number:<br>
             <input type=text name="qtext" value="" />
             <input type="submit" value="SEARCH" />&nbsp;
             <input type="reset" value="RESET" /><br />

print "Content-Type: text/html\n\n"
print "<head><title>Searching by using Shot Number</title></head><body>"

if __name__ == "__main__":
    data = cgi.FieldStorage()

    if data:
        qtype = data.getfirst('qtype')
        qtext = data.getfirst('qtext','')

        for qtype in data.getlist('qtype'):
            print "You typed in Shot Number:", qtext

            #Now, we can get to the database...
            username = os.environ.get('USER')
            if username == None:
                username = 'apache'
            db = pg.connect("moncdata", user=username, passwd=None)
            query = "select %(qtype)s from shot_descriptions where shot_number=%(qtext)s", qtype, qtext
            qresult = db.query(query)

            listOfResults = qresult.dictresult()

            print """<p>Example of pulling the list of dictionary results apart.</p>"""
            for record in listOfResults:
                print "<p><table>"
                for k in record.keys():
                    print '<tr>'
                    print '<td>key:</td> <td>', k, '</td>'
                    print '<td>value:</td><td>', record[k], '</td>'
                    print '</tr>'
                print '</table></p>'


            print "You have no enter a shot number!"
            print "Please type a in shot number in order to perform a search!"


print "</body></html>"

More information about the Tutor mailing list