[Tutor] Input from CGI ?
Rene Lopez
renx99 at gmail.com
Fri Nov 5 01:49:01 CET 2004
Okay here's how I get data from my forms in my cgi scripts... the
quick and dirty method:
#!/usr/bin/python
"""A small program to demonstrate simple CGIish stuff."""
import cgi
import cgitb
form = FieldStorage()
if form.has_key("foo"):
print "woohoo"
if form.has_key("goo"):
print "wup dee do"
etc etc etc.
How when you create your form and submit, all the info gets sent to
the cgi script in FieldStorage. Doing a form = FieldStorage() puts it
all in the form variable. All the info that the form submitted should
be there.
for example, I have a form that submits answer1 with a value of 2 and
answer2 with a value of 50... if i want to get these values in my
script i do:
myanswer = form("answer1").value #assigns the value of "answer1" from
the form....
hopefully this makes sense to you... I'm notorious for giving bad
instructions ;-)
Rene
On Thu, 4 Nov 2004 20:04:01 +0200, Mark Kels <mark.kels at gmail.com> wrote:
> On Wed, 3 Nov 2004 13:34:46 -0800 (PST), Danny Yoo
>
>
> <dyoo at hkn.eecs.berkeley.edu> wrote:
> >
> >
> > On Wed, 3 Nov 2004, Mark Kels wrote:
> >
> > > How can I get input (string) from the user in CGI ?
> >
> >
> > Hi Mark,
> >
> > There are examples of doing this in the Standard Library documentation.
> > Here's a link to the documentation on the 'cgi' module:
> >
> > http://www.python.org/doc/lib/module-cgi.html
> >
> > The idea is to use the cgi module to grab form values. There are several
> > methods we can use, but the easiest one is probably
> > FieldStorage.getfirst().
> >
> > Here's a quick example CGI program that takes in a number and prints out
> > its square:
> >
> > ###
> > #!/usr/bin/python
> >
> > """A small program to demonstrate simple CGIish stuff."""
> >
> > import cgi
> > import cgitb; cgitb.enable()
> >
> > def main():
> > form = cgi.FieldStorage()
> > if form.getfirst("number"):
> > doComputationAndPrintResult(form)
> > else:
> > printStartPage()
> >
> > def printStartPage():
> > printHtmlHeader()
> > print """
> > <html><body>
> > <form><input name="number" type="text"></form>
> > </body></html>
> > """
> >
> > def doComputationAndPrintResult(form):
> > computedResult = square(int(form.getfirst("number")))
> >
> > printHtmlHeader()
> > print """
> > <html><body>
> > %s
> > </body></html>
> > """ % computedResult
> >
> > def printHtmlHeader():
> > """Prints out the standard HTML header line."""
> > print "Content-type: text/html\n\n"
> >
> > def square(x):
> > """Returns the square of x."""
> > return x * x
> >
> > if __name__ == '__main__':
> > main()
> > ###
> >
> > You might also like to browse through the Web Programming topic guide
> > here:
> >
> > http://www.python.org/topics/web/
> >
> > Good luck to you!
> >
> >
>
> Thank you!!
> It was very helpful.
>
> But how do I get more than one line ?
> And how can I make radio buttons and get the input wuth them ?
> And what this thing does ( I see it in almost every code example) :
> if __name__ == '__main__':
> main()
> ?
>
> Thanks!!
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
Rene
More information about the Tutor
mailing list