if information exists on form then use it in cgi

Lee Harr missive at frontiernet.net
Mon Jul 21 17:34:09 EDT 2003


In article <2c6431ab.0307211144.4c2f3749 at posting.google.com>, lamar_air wrote:
> On my web form i have check boxes with the following code:
> 
><td width="173"><input type="checkbox" name="C140" value="1.
> 'St.KittsNevis'">St. Kitts Nevis</td>
> 
> when the user submits the form my python cgi retreives each of the
> values from the form with code like this for each:
> 
>     f=open('C:\My Documents\ABC\boxes', 'w')
> 
>     f.write('Boxes')
>     f.write('\n')



Is form a dictionary?  How about:

    if form.has_key('C139') and form['C139'] != '':


Also, if you have a bunch of form elements like this, 
you will probably want to process them in a loop:

for k in form.keys():  # may want to sort them first...
    if k.startswith('C'):  # or some other way to identify them...
        f.write(form[k].value)



>     if form['C139'].value != '':
>         f.write(form['C139'].value)
>         f.write('\n')
> 
>     if form['C140'].value != '':
>         f.write(form['C140'].value)
>         f.write('\n')
>     
>     f.close()
> 
> the problem is if the user doesn't check off all the check boxes on
> the form then the if form['C140'].value != '' code errors because
> nothing is there.  How do i do this?
> 
> Here is the error:
> 
> The specified CGI application misbehaved by not returning a complete
> set of HTTP headers. The headers it did return are:
> 
> 
> Traceback (most recent call last):
>   File "C:\Inetpub\wwwroot\Cgi-bin\Cities2.py", line 175, in ?
>     if form['C139'].value != '':
>   File "C:\Python22\lib\cgi.py", line 550, in __getitem__
>     raise KeyError, key
> KeyError: C139




More information about the Python-list mailing list