How can I code to get more than One value from checkbox?

Shufen s4046441 at student.uq.edu.au
Fri Oct 1 02:29:37 EDT 2004


Steve Holden <steve at holdenweb.com> wrote in message news:<6J07d.1812$gk.1795 at okepread01>...
 
> I think you're confusing checkboxes and radio buttons.
> 
> If you have ten different boxes that can be checked, each will usually 
> have a different name, so you can test them with data.getfirst("box1"), 
> data.getfirst("box2"), and so on.
> 
> While it's true that you can have several checkbox elements with the 
> same name, and that the CGI interface will give you a list of such 
> values, there's absolutely no need to do so and, as you have discovered, 
> it's positively unhelpful in most cases.
> 
> With radio buttons, you give each member of the set the same name in 
> your HTML and the browser submits the value of only the one that's been 
> checked (if one has).
> 
> regards
>   Steve

Hi Steve,

I did that with reference to this:
http://www.python.org/doc/current/lib/node404.html

I used checkboxes because I wanna the user to be able to select one or
more of the checkboxes, these checkboxes have the same "name" - qtype
and different "value". For radiobuttons, the user can only choose one
of the given radiobuttons which is not what I want. Sorry, I think I
didn't make myself clear enough.


The following is my script:

###############################################################################
#!/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="">
             <p>
             <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" /> 
             <input type="reset" value="RESET" /><br />
             </form>"""

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

            #Will take care of the security problem later...
            username = os.environ.get('USER')
            if username == None:
                username = 'apache'

            #Now, we can get to the database...
            db = pg.connect("moncdata", user=username, passwd=None)

            #This is the part that I'm not sure of...please help...
            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>'

            db.close()

        #Somehow, this part didn't work too, when I didn't input a
value
        #it doesn't show this msg. Please help me on this part too.
Thks.
        else:
            print "You have no enter a shot number!"
            print "Please type a in shot number in order to perform a
search!"

    else:
        form()


print "</body></html>"
    
###############################################################################

Thank you in advance for any help.

Shufen



More information about the Python-list mailing list