cgi form validation problems
Steve Holden
steve at holdenweb.com
Mon Aug 15 07:46:53 EDT 2005
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/
More information about the Python-list
mailing list