Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   if information exists on form then use it in cgi (http://www.velocityreviews.com/forums/t319960-if-information-exists-on-form-then-use-it-in-cgi.html)

lamar_air 07-21-2003 07:44 PM

if information exists on form then use it in cgi
 
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')
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

Lee Harr 07-21-2003 09:34 PM

Re: if information exists on form then use it in cgi
 
In article <2c6431ab.0307211144.4c2f3749@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



All times are GMT. The time now is 08:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.