CGI multiple select?

Steve Holden sholden at holdenweb.com
Mon Apr 8 18:26:57 EDT 2002


"John" <nobody at nobody.com> wrote in message
news:56ns8.37728$tg4.448055 at vixen.cso.uiuc.edu...
>
> Hi,
>
> In my html form, I have 'multiple select'
>
> <select name=spred multiple>
> <option..>
> <option..>
>
>
> This form invokes CGI routine, where I try to read selected entries from
> form["spred"]
>
> My dilemma is here:
>
> When only one entry is selected:
> form["spred"].value (will be the object selected)
>
> When multiple entries were selected:
> for o in form["spred"]:
>   o.value (--> each of these will be the object selected)
>
> Thus, I need to handle according to situation.. I though my previous
> question on 'isList' can solve this matter, but somehow, all solution
> suggested (isSequenceType, type, ..) cannot distinguish these two cases.
>
I believe the following, from the Library Reference Manual, section 11.2.2,
will apply (but I haven't tested this today):

"""
If the submitted form data contains more than one field with the same name,
the object retrieved by "form[key]" is not a FieldStorage or
MiniFieldStorage instance but a list of such instances. Similarly, in this
situation, "form.getvalue(key)" would return a list of strings. If you
expect this possibility (when your HTML form contains multiple fields with
the same name), use the type() built-in function to determine whether you
have a single instance or a list of instances. For example, this code
concatenates any number of username fields, separated by commas:

from types import ListType

value = form.getvalue("username", "")
if isinstance(value, ListType):
    # Multiple username fields specified
    usernames = ",".join(value)
else:
    # Single or no username field specified
    usernames = value
"""

regards
 Steve






More information about the Python-list mailing list