if information exists on form then use it in cgi

Discussion in 'Python' started by lamar_air, Jul 21, 2003.

  1. lamar_air

    lamar_air Guest

    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
     
    lamar_air, Jul 21, 2003
    #1
    1. Advertising

  2. lamar_air

    Lee Harr Guest

    In article <>, 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
     
    Lee Harr, Jul 21, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cG11ZA==?=

    Validating: If the value EXISTS in Db then show error!

    =?Utf-8?B?cG11ZA==?=, Jan 25, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    934
    =?Utf-8?B?cG11ZA==?=
    Jan 27, 2005
  2. Totan
    Replies:
    0
    Views:
    979
    Totan
    Apr 17, 2006
  3. Ulf Meinhardt
    Replies:
    8
    Views:
    6,161
  4. Replies:
    0
    Views:
    297
  5. Guy
    Replies:
    4
    Views:
    824
    Malcolm Dew-Jones
    Dec 11, 2003
Loading...

Share This Page