Creating Self-Posting CGI Scripts : form « CGI Web « Python Tutorial






import cgi, os, sys

sys.stderr = sys.stdout

data = cgi.FieldStorage()

formText = """Content-type: text/html\n
<title>CGI Self-Post Form</title>\n
<h2>Enter Quote</h2><Paragraph>
<form method="POST" action="cgi_selfpost.cgi">
    Name <input type="TEXT" name="name">
    <paragraph>
    Quote <input type="TEXT" name="quote" size="80">
    <paragraph>
    <input type="SUBMIT" value="send">
</form>
<hr>
<h2>Received Quotes</h2><paragraph>"""
print formText

if data.has_key('name') and data.has_key('quote'):
    f = open("quotes.dat", 'a')
    f.write("<li><b>%s:</b> %s</li>\n" % \
            (data['name'].value,
data['quote'].value))
    f.close()

f=open("quotes.dat", 'r')
if f:
    print f.read()
    f.close()








22.3.form
22.3.1.A Simple Form
22.3.2.Creating Self-Posting CGI Scripts
22.3.3.Static Form Web Page (friends.htm)
22.3.4.Demonstrates get method with an XHTML form
22.3.5.Demonstrates post method with an XHTML form
22.3.6.Demonstrates use of cgi.FieldStorage with an XHTML form
22.3.7.List form data
22.3.8.Login form
22.3.9.Receiving Data from an HTML File