if information exists on form then use it in cgi

L

lamar_air

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
 
L

Lee Harr

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top