[Tutor] Another CGI question

dman dman@dman.ddts.net
Sat, 25 May 2002 17:50:44 -0500


--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat, May 25, 2002 at 02:41:24PM -0700, Britt Green wrote:
| Thanks to everyone who gave me advice on starting some CGI programming
| in Python. You've all been very helpful.
|=20
| I did have another question that I've been unable to find the answer
| to. Suppose my web page has check boxes. A visitor selects multiple
| check boxes. What does the Python code look like that would return
| multiple values? So far I've only been able to find examples for things
| like radiobuttons and text fields. Both of those return a single value.

Checkboxes don't return multiple values.  Here's what I mean --
Suppose you have this form :

<form action=3D"mycgi.py">
    <input type=3D"checkbox" name=3D"box1" > Box1
    <input type=3D"checkbox" name=3D"box2" > Box2
</form>


This form has 2 independent elements, one is named "box1" and can
either be set or unset, the other is named "box2" and can be set or
unset.  Unfortunately, when a checkbox is unset it "doesn't exist"
(IIRC).

Here's how you can check them (untested) :


import cgi

# here's where the details of parsing the arguments from the client
# are taken care of!  thus the details are unimportant to you
the_form =3D cgi.FieldStorage()

if the_form.has_key( "box1" ) :
    print "Box1 was selected!"
else ;
    print "Box1 was NOT selected!"

if the_form.has_key( "box2" ) :
    print "Box2 was selected!"
else ;
    print "Box2 was NOT selected!"



-D

--=20

How to shoot yourself in the foot with Java:

You find that Microsoft and Sun have released incompatible class
libraries both implementing Gun objects. You then find that although
there are plenty of feet objects implemented in the past in many other
languages, you cannot get access to one. But seeing as Java is so cool,
you don't care and go around shooting anything else you can find.
    (written by Mark Hammond)
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--BXVAT5kNtrzKuDFl
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjzwFUQACgkQO8l8XBKTpRR56gCeLawDB6Cdjh+qVjI2FtyUrVMO
ElQAoLfkJyoMxIePwRE4wF/kjXFdygm3
=TqGt
-----END PGP SIGNATURE-----

--BXVAT5kNtrzKuDFl--