cgi getlist() problem (my first python program)

John J. Lee jjl at pobox.com
Fri Jun 20 03:09:59 CEST 2003


Gobo Borz <goboborz at yahoo.com> writes:
[...]
> Being new to Python, it was good to learn this wasn't an oversight in
> the Python cgi module. I determined I can work around the problem by
> using the clumsy method of inserting a placeholder character in each
> blank field on the client side with javascript.  This should work
> unless the browser mangles the order of the fields.

But why do that?  Clumsy, as you say.


>  > 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>
> 
> I didn't want to use this approach because I thought I would have to
> use eval() or something like that on the server side to build the
> field name during iteration.

eval??  I must be misunderstanding what your problem is.  You do
realise that variable names in a program don't have to match the names
you put in your markup, right?


> Maybe there's an easier way in Python to
> iterate through a list of names like this,

...or in any language!  If I understand your problem correctly, you
just iterate over a range of integers.

print "<form>"

nr_people = 2

for i in range(nr_people):
    print "  <td>"
    print '    <input name="name%d">' % i
    print '    <input name="phone%d">' % i
    print "  </td>"

print "</form>"


> but I didn't find any
> examples when I looked in the manual and searched google.  Hopefully

Why *would* you expect to find an example that exactly matches your
particular problem?


John




More information about the Python-list mailing list