cgi getlist() problem (my first python program)

Alan Kennedy alanmk at hotmail.com
Thu Jun 19 11:44:30 EDT 2003


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