cgi getlist() problem (my first python program)

Gobo Borz goboborz at yahoo.com
Thu Jun 19 16:01:38 EDT 2003


Thanks Alan - I appreciate your spending the time to give a complete and 
helpful answer !

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.

 >Once any value is placed in a form field, even
 > invisible whitespace, it should then be considered a "successful
 > control"

You're right, I was reading the output wrong.


 > 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. 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.  Hopefully it's just being new 
to the language. If anyone knows how to do this I would be very grateful 
if you would share it with me, as my solution of using placeholders is ugly.


Again, I really appreciate the help!  -Gobo

--------------------------------------------------------------
Alan Kennedy wrote:
> Gobo Borz wrote:
> 
> 
>>This is my first Python program, and I'm having problems with  cgi
>>getlist() skipping values in a multi-row html form.
> 
> 
>>When I use getlist() to
>>get the field values, it skips empty fields. Thus, phone[4], for
>>example, may  turn out to be in a different row than name[4],
>>depending on which fields the user has left blank.
> 
> 
> This is as it should be, according to the HTML 4.01 spec.
> 
> http://www.w3.org/TR/html401/interact/forms.html#submit-format
> 
> The relevant section is "17.13.2 Successful Controls", which says
> 
> "A successful control is "valid" for submission. Every successful
> control has its control name paired with its current value as part of
> the submitted form data set. A successful control must be defined
> within a FORM element and must have a control name."
> 
> and later in the same section
> 
> "If a control doesn't have a current value when the form is submitted,
> user agents are not required to treat it as a successful control"
> 
> So different behaviour is to be expected from different browsers. You
> should test submitting your form from different browsers.
> 
> 
>>Even if I force a space in the field with javascript on the client
>>side, the python FOR loop skips the blank values. 
> 
> 
> Hmm, this is odd. 
> 
> Once any value is placed in a form field, even
> invisible whitespace, it should then be considered a "successful
> control". Are you sure that your javascript is working here? One
> way to test is to see what happens in your CGI script when you 
> *manually* enter a space into what would otherwise be empty fields.
> 
> When you do this, does it behave in the same way as when you fill the
> empty fields from javascript?
> 
> 
>>I hope I'm not missing something obvious. It seems like this would
>>be a common problem for anyone coding a multi-row html form.
> 
> 
> Unfortunately, there is no way for the cgi module to know how many
> name=value pairs are *supposed* to be in a list of field values: the
> browser is only obliged to transmit fields that are "successful".
> 
> 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>
> 
> Should be easy enough to do if you're generating your form from the cgi
> script itself. If you're using a static form, just add the numbers in
> by hand.
> 
> 
>>Thanks for your help,
> 
> 
> If none of this helps, and you need to post again, it is always
> helpful to post snippets of code from your script. That way people can
> test your code by running it as well as just looking at it. But do try
> to reduce the problem to its minimal representation (the process of
> which often helps you to solve the problem :-)
> 
> HTH,
> 
> --
> alan kennedy
> -----------------------------------------------------
> check http headers here: http://xhaus.com/headers
> email alan:              http://xhaus.com/mailto/alan





More information about the Python-list mailing list