cgi getlist() problem (my first python program)

Max M maxm at mxm.dk
Fri Jun 20 04:32:20 EDT 2003


Gobo Borz wrote:

>  > One way to approach the problem is by numbering the fields in your
>  > HTML form, like so
>  >
>  > <form>
>  >   <td>
>  >     <input name="name1">
>  >     <input name="phone1">
>  >   </td>
>  >   <td>
>  >     <input name="name2">
>  >     <input name="phone2">
>  >   </td>
>  > </form>
> 
> Maybe there's an easier way in Python to iterate 
> through a list of names like this, but I didn't find any examples when I 
> looked in the manual and searched google.


A simple way is to render the indexes of every item as a hidden field:

<form>
   <td>
     <input type="hidden" name="indexes" value="1">
     <input name="name1">
     <input name="phone1">
   </td>
   <td>
     <input type="hidden" name="indexes" value="2">
     <input name="name2">
     <input name="phone2">
   </td>
</form>


results = []
for idx in cgi['indexes'].value:
     name = cgi.get('name%s' % idx, '')
     phone = cgi.get('phone%s' % idx, '')
results.append((name, phone))


regards Max M





More information about the Python-list mailing list