cgi form validation problems

Discussion in 'Python' started by googleboy, Aug 14, 2005.

  1. googleboy

    googleboy Guest

    Hi.

    I am writing up my own web form. I'm a bit of a newb, and read up many
    different how-tos and examples and documentaion. I finally had it
    working just great until I decided that I wanted to add some extra
    logic because there's one form that submits a particular type of
    information. a little extra validation, and the creation of a list of
    the values to be mailed in to the site manager.

    The code below is where I am going wrong (edited for brevity):


    form=cgi.FieldStorage()

    rev_fields = { "param1":None, "param3":None, "param6":None,
    "param5":None, "param8":None, "param9":None, "param10":None,
    "param11":None }


    # Everything worked until I added the following if statement:

    if form.has_key("param8"): # Only one form has this
    param8 = form.getvalue("param8")
    if param8 == 0: # 0 is the default value
    print "Content-type: text/html"
    debug("You must give the item a rating")
    for field in form.keys():
    value = form[field].value
    if rev_fields.has_key(field):
    rev_fields[field] = value
    for key in rev_fields:
    if rev_fields[key] == None:
    print "Content-type: text/html"
    debug("All fields must be filled in. Please check your %s
    submission." % key)
    else:
    #feedback = ("%s, %s, %s, %s, %s, %s, %s, %s" %
    (form["param1"].value, form["param3"].value, form["param6"].
    value, form["param5"].value, form["param8"].value,
    form["param9"].value, form["param10"].value, form["param11"].value)
    #feedback = ("%s, %s, %s, %s, %s, %s, %s, %s" %
    (form.getvalue("param1"), form.getvalue("param3"), form.getvalue(
    "param6"), form.getvalue("param5"), form.getvalue("param8"),
    form.getvalue("param9"), form.getvalue("param10"),
    form.getvalue("param11"))
    feedback = ("%s, %s, %s, %s, %s, %s" %
    (rev_fields["param1"], rev_fields["param3"], rev_fields["param6"],
    rev_fields["param5"], rev_fields["param8"], rev_fields["param9"],
    rev_fields["param10"], rev_fields["param11"])


    #feedback = form[ "score" ].value

    msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n Feedback: %s\r\n\r\n"
    %
    (rev_fields["param2"], rev_fields["param7"], rev_fields["param3"],
    rev_fields["param6"]))


    If I comment out the 'else:' logic, it works great. But then I don't
    get a list called feedback containing all teh bits I want, The error I
    get is really strange, too:

    [Mon Aug 15 05:54:58 2005] [error] [client 60.224.106.116] Premature
    end of script headers: /var/www/users/senta/html/gobooks/cgi/form.py
    File "/var/www/users/senta/html/gobooks/cgi/form.py", line 99
    msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n Feedback:
    %s\r\n\r\n" %
    ^
    SyntaxError: invalid syntax

    Just a simple assignation.... I did think it might have been an
    indentation error, but I changed that around and got a message telling
    me about an indentation problem, which this doesn't do.

    I have tried several different ways to assign the values, as you can
    see by the commented out lines. Tried getting the values directly from
    teh form, and also from the validated rev_fields dictionary. I'd be
    extremely grateful to anyone who helps me through this.

    TIA

    Googleboy
     
    googleboy, Aug 14, 2005
    #1
    1. Advertising

  2. googleboy

    Steve Holden Guest

    googleboy wrote:
    > Hi.
    >
    > I am writing up my own web form. I'm a bit of a newb, and read up many
    > different how-tos and examples and documentaion. I finally had it
    > working just great until I decided that I wanted to add some extra
    > logic because there's one form that submits a particular type of
    > information. a little extra validation, and the creation of a list of
    > the values to be mailed in to the site manager.
    >
    > The code below is where I am going wrong (edited for brevity):
    >
    >
    > form=cgi.FieldStorage()
    >
    > rev_fields = { "param1":None, "param3":None, "param6":None,
    > "param5":None, "param8":None, "param9":None, "param10":None,
    > "param11":None }
    >
    >
    > # Everything worked until I added the following if statement:
    >
    > if form.has_key("param8"): # Only one form has this
    > param8 = form.getvalue("param8")
    > if param8 == 0: # 0 is the default value
    > print "Content-type: text/html"
    > debug("You must give the item a rating")
    > for field in form.keys():
    > value = form[field].value
    > if rev_fields.has_key(field):
    > rev_fields[field] = value
    > for key in rev_fields:
    > if rev_fields[key] == None:
    > print "Content-type: text/html"
    > debug("All fields must be filled in. Please check your %s
    > submission." % key)
    > else:
    > #feedback = ("%s, %s, %s, %s, %s, %s, %s, %s" %
    > (form["param1"].value, form["param3"].value, form["param6"].
    > value, form["param5"].value, form["param8"].value,
    > form["param9"].value, form["param10"].value, form["param11"].value)
    > #feedback = ("%s, %s, %s, %s, %s, %s, %s, %s" %
    > (form.getvalue("param1"), form.getvalue("param3"), form.getvalue(
    > "param6"), form.getvalue("param5"), form.getvalue("param8"),
    > form.getvalue("param9"), form.getvalue("param10"),
    > form.getvalue("param11"))
    > feedback = ("%s, %s, %s, %s, %s, %s" %
    > (rev_fields["param1"], rev_fields["param3"], rev_fields["param6"],
    > rev_fields["param5"], rev_fields["param8"], rev_fields["param9"],
    > rev_fields["param10"], rev_fields["param11"])


    I believe you missed a closing parenthesis, which is why you are getting
    the syntax error - the scanner has to look at the next source line
    because it expects it to continue the unclosed expression. That's why
    you are getting confused.
    >
    >
    > #feedback = form[ "score" ].value
    >
    > msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n Feedback: %s\r\n\r\n"
    > %
    > (rev_fields["param2"], rev_fields["param7"], rev_fields["param3"],
    > rev_fields["param6"]))
    >

    Since rev_fields is a dictionary, it would be easier to write this as

    msg = "From %(param2)s\r\nTo: %(param7)\r\nSubject: %(param3)s\r\n\r\n
    Feedback: %(param6)s\r\n\r\n" % rev_fields

    module any text wrapping the mailstream might have done to the above
    single line.

    >
    > If I comment out the 'else:' logic, it works great. But then I don't
    > get a list called feedback containing all teh bits I want, The error I
    > get is really strange, too:
    >
    > [Mon Aug 15 05:54:58 2005] [error] [client 60.224.106.116] Premature
    > end of script headers: /var/www/users/senta/html/gobooks/cgi/form.py
    > File "/var/www/users/senta/html/gobooks/cgi/form.py", line 99
    > msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n Feedback:
    > %s\r\n\r\n" %
    > ^
    > SyntaxError: invalid syntax
    >
    > Just a simple assignation.... I did think it might have been an
    > indentation error, but I changed that around and got a message telling
    > me about an indentation problem, which this doesn't do.
    >
    > I have tried several different ways to assign the values, as you can
    > see by the commented out lines. Tried getting the values directly from
    > teh form, and also from the validated rev_fields dictionary. I'd be
    > extremely grateful to anyone who helps me through this.
    >
    > TIA
    >
    > Googleboy
    >


    Hope this helps.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Aug 15, 2005
    #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. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    433
    Tommy
    Nov 29, 2003
  2. Sumith Mathur
    Replies:
    0
    Views:
    2,219
    Sumith Mathur
    Sep 23, 2004
  3. OriginalBrownster

    string validation/ form validation

    OriginalBrownster, Aug 21, 2006, in forum: Python
    Replies:
    1
    Views:
    429
    Simon Forman
    Aug 21, 2006
  4. Stefan Fischer
    Replies:
    2
    Views:
    312
    Stefan Fischer
    Feb 23, 2010
  5. bnp
    Replies:
    4
    Views:
    333
Loading...

Share This Page