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

Steve Holden steve at holdenweb.com
Fri Oct 1 08:20:07 EDT 2004


Shufen wrote:

> 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>
> 
OK, it's not really obvious what this form is supposed to do for you. I 
also don't understand whether it makes sense for "all" to be checked at 
the same time as individual selections. But we'll leave that for now ...

>              <p>Search by shot number:<br>
>              <input type=text name="qtext" value="" />

It might make more sense to call this input "shotno" rather than 
"qtext", but that won;t affect how the code functions.

>              <input type="submit" value="SEARCH" /> 
>              <input type="reset" value="RESET" /><br />
>              </form>"""
> 
Aah, so we're looking up photographs in a database?

> print "Content-Type: text/html\n\n"
> print "<head><title>Searching by using Shot
> Number</title></head><body>"
> 
> if __name__ == "__main__":
>     
It's a bit too late for this if you've already written the headers out, 
I should say - it would make more sense to move those prints above down 
here so they don't get run if the script is imported.

>     data = cgi.FieldStorage()
> 
Great!

>     if data:
>         qtype = data.getfirst('qtype')

Since the "for" statement below assigns to qtype, the statement above is 
completely redundant.

>         qtext = data.getfirst('qtext','')
> 
>         for qtype in data.getlist('qtype'):

So if you check "project" and "blame", you should iterate round this 
loop twice, with qtype being set to "project" the first time and "blame" 
the second.

>             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)
> 
It doesn't make sense to connect to the database each time round the 
loop (and I'm still not even convinced this loop is what you want...)

>             #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)
> 
I take it pg is some kind of PostGres interface? And AAH!!, now I see 
that what the checkboxes are supposed to do is select fields for the 
output. You appear to be missing a "percent" sign up there - it should 
either be

     query = "SELECT %s ...=%s" % (qtype, qtext)

or

     query = "SELECT %(qtype)s ... =%(qtext)s" % data

to put the query type and the text into the string, depending on which 
substitution method you want to use. You should really be testing a lot 
of this stuff in an interactive copy of the interpreter (I hope you do 
have Python available other than just on your web  server).

Unfortunately the assignment to query isn;t a syntax error in Python 
(you are just assigning a three-element tuple to query) but poor old 
PostgreSQL isn;t going to know what to make of the result.

Do you want to see a separate chunk of output for each field, or do you 
want to see the fields as columns of a table? The latter seems to make 
more sense to me, but it would have been REALLY nice if this 
conversation has started out like "I have a bunch of shot descriptions 
is a database, and I want to be able to see just selected fields about a 
shot".

For the structure you've got now it looks like you plan to loop around 
the column names, doing a query for each one, and printing out a table 
for each field. And we don't have any way to handle "all", by the way 
... unless that's just another database column.

>             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.

The reason for that is that even when you don't enter anything into the 
form the submit button still gets put in there as a data item, so the 
form will never be truly empty.

>         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

OK, before we go any further I'd like you to tell me exactly what you;d 
like this page to do. You probably aren't that far froma solution now, 
but it would be nice to answer the right question.

I'm suspecting that what you really want is something like

     query = """SELECT %s FROM shot-descriptions WHERE
                   shot_number=%s""" % (
			data.getvalue("qtype"), data.getfirst("qtext"))

Since I have to go off and earn pennies now, someone else may be able to 
help you in the interim. I'm likely to be off-net for a bit. Stick with 
it, you're almost there!

regards
  Steve



More information about the Python-list mailing list