How can I code to get more than One value from checkbox? (Revised Version)

Shufen s4046441 at student.uq.edu.au
Sun Oct 3 03:33:06 CEST 2004


Hi,

Sorry I know this is kinda of long, but please give me a helping hand
if you understand what I'm looking for.

The following attached is my revised version of my code. I added some
stuffs into it and now it can really run - error (I supposed my method
is wrong again, but I hope my idea is there).

This is what is I have:
I have a bunch of stuffs in a database table named
"shot_descriptions".

And this is what I wanna my page to do:
The columns of this table consists of project name, date, shot number,
notes and etc. And these fields are the stuffs that I wanna to present
them in checkboxes form so that the user can select the number of
information they wanna to see in the results (output). And a textbox
for user to key in a specific shot number in order to perform a
search. So, if there is no input in the shot no textbox, an error msg
will show (this is one of the part that is not working now, please
help). I wanna to display the output in a table form and I'm currently
using dictionary to help me with this task. But I'm wondering is there
a better way to show it? I mean other than dictionary, how can I code
it so that the results will display nicely in a html type of table
with columns and rows instead of key: blah blah and value: blah blah.
If someone can help me on this part, it will be very nice, cos I'm
concern about the presentation of the output too.

Revised Version:
What I did is include a section where it will display an error msg
when no input is given. Besides, my code (earlier version) displayed
results in a repeated loop form when there is more than a tick in the
checkboxes.

It will show somthing like this:
You entered Shot Number: xxxx

key: xxxx   value: xxxx

You entered Shot Number: xxxx

key: xxxx   value: xxxx

And it just went on....depending the number of ticks.

Thus, in this revised version, I separated into 2 conditions.
Condition 1 taking care of more than one input and Condition 2 taking
care of only ONE input. But somehow, there is error in this code and I
suspected I did something very wrong again so please help me. I wonder
if there is any way to display the results in a nice table form for
Input that is more than ONE?

Any help is very very much appreciated. 

I hope that will be someone responding to this as I took a long time
to figure out this revised code (I'm not good in programming - new)
and of cos to write this msg.

Thanks for any help.

Shufen


###############################################################################
#!/usr/bin/env python
#Author: Chong Soo Fern
#Created on: 30/09/04
#Modified on: 02/10/04
#Help from Lee Harr, Danny Yoo and Steve - Python Tutor

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

def form():
    print """<form method="post" action="">
           <table border="0" width="750" cellspacing="2"
cellpadding="2">
           <tr><td width="150" align="left">
           <input type=checkbox name="qtype" value="*" checked
/>All</td>
           <td width="150" align="left">
           <input type=checkbox name="qtype" value="project"
/>Project</td>
           <td width="150" align="left">
           <input type=checkbox name="qtype" value="notes"
/>Notes</td>
           <td width="150" align="left">
           <input type=checkbox name="qtype" value="date_string"
/>Date</td>
           </table>
       
         <p>Search by shot number:<br>
         <input type=text name="shot_no" value="" tabindex="1" />
         <input type="submit" value="SEARCH" tabindex="2" /> 
         <input type="reset" value="RESET" tabindex="3" /><br />
         </form>"""
             
          
if __name__ == "__main__":
    
    print "Content-Type: text/html\n\n"
    print "<head><title>Quick Search using Run
No</title></head><body>"
    
    sys.stderr = sys.stdout
    data = cgi.FieldStorage()

    #Get the shot_no first, followed by each value of the checkboxes.
    shot_no = data.getfirst("shot_no", "")
    qtype = data.getvalue("qtype")


    #I wanna to show the error msg if no input is given.
    if not (data.has_key("qtype") and data.has_key("shot_no")):
        print "You have not select an option!\n"
        print "Please tick at least one checkbox provided!\n"
        print "You have no enter a shot number\n!"
        print "Please type a in shot number in order to perform a
search\n!"

        #I don't know why this function kept showing error... please
advice
        #Error: SyntaxError: 'return' outside function 
        return form() 
    
    if isinstance(qtype, list):

        # Conditon 1: The user selected more than one qtype.
        # Display the results in one way, different from condition 2.

        print "You entered Shot Number:", shot_no
        
        #Will take care of the security problems later...
        #Defined a username and connect to the database...
        username = os.environ.get('USER')
        if username == "None":
            username = "apache"

        db = pg.connect("moncdata", user=username, passwd=None)
        query = """SELECT %s FROM shot_descriptions WHERE
shot_number=%s""" % (qtype, shot_no)
        qresult = db.query(query)
        listOfResults = qresult.dictresult()
        print listOfResults
        
        db.close()

    else:

        # Condition 2: The user selected only ONE qtype.
        # Display the results in another way.
        # I don't want each qtype to showed in sort of a repeated
results loop way.
        
        print "You entered Shot Number:", shot_no
        
        #Will take care of the security problems later...
        #Defined a username and connect to the database...
        username = os.environ.get('USER')
        if username == "None":
            username = "apache"

        db = pg.connect("moncdata", user=username, passwd=None)
        query = """SELECT %s FROM shot_descriptions WHERE
shot_number=%s""" % (qtype, shot_no)
        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 '<br />'
                print '<td>value:</td><td>', record[k], '</td>'
                print '</tr>'
                print '</table></p>'

        db.close()
                     
print "</body></html>"

###############################################################################



More information about the Python-list mailing list