newbie form questions...

tefol tefol at This.Is.Not.An.Address
Sun Oct 7 08:12:46 EDT 2001


Hiya everyone....

I am trying to get a form to create a file (or append to a file if the file 
already exists, maybe..  haven't quite decided what to do in case the form 
is processed too foten to quickly yet) on a webserver as a result of a POST 
to a http form.  How I want the file to look is something like this:

field1=stringOfField1
field2=stringOfField2
field3=stringOfField3
field4=stringOfField4
field5=stringOfField5

or alternatively, 

field1 stringOfField1
field2 stringOfField2
field3 stringOfField3
field4 stringOfField4
field5 stringOfField5

But what I get is this:

(i__main__
FeedbackData
p0
(dp1
S'field5'
p2
S'stringOfField5'
p3
sS'field4'
p4
S'stringOfField4'
p5
sS'field1'
p6
S'stringOfField1'
p7
sS'field3'
p8
S'stringOfField3'
p9
sS'field2'
p10
S'stringOfField2'
p11


What the heck is all that stuff?   Is there a way to make the output be as 
I require?  Or should I create some huge regex thing to process this output 
so that it appears as I want??

Or is there an entirely different approach to take?  Basically,  I want a 
script to create a file that a cron script will regularly search for.  If 
it finds it,  it should process the information accordingly.   My script 
and html follow.  

TIA

Tefol

+++++++++++


FORM HTML:

<HTML>
<BODY bgcolor="#000000" text="#FFFF00" link="#0099FF" vlink="#0099FF" 
alink="#0099FF">


<br><br><br>

<center><h2>Python Form Test</h></center>

<br><br>

All fields are required, or else the form funtion cannot proceed.

<br><br>

<FORM METHOD=POST ACTION='cgi-bin/inputform.py'>

<center><table width="80%" >
    <tr><td>Field1</td>
        <td><input type=text name=field1 size=50 value=""></td></tr>
    <tr><td>Field2</td>
        <td><input type=text name=field2 size=50 value=""></td></tr>
    <tr><td>Field3</td>
        <td><input type=text name=field3 size=50 value=""></td></tr>
    <tr><td>Field4</td>
        <td><input type=text name=field4 size=50 value=""></td></tr>
    <tr><td>Field5</td>
        <td><input type=text name=field5 size=50 
value="email at address.com></td></tr>
    <tr><td></td>
    <td><input type=submit name=send value="process the user"</td></tr>
</table></center>
</form>

</body>
<html>


++++++++

Python script.


#!/usr/bin/python2

import cgi, os, sys, string, pickle

def bail():
    print "Content-type: text/html\n\n"
    print "<h3>You didn't fill something out</H3>"
    print "<br><br>Double check each field!<br>"
    print "Press the 'Back' button to do it again."
    sys.exit()

class FormData:
    '''CGI Form information'''
    def __init__(self, form):
        for fieldname in self.fieldnames:
            if not form.has_key(fieldname) or form[fieldname].value == "":
                bail()
            else:
                setattr(self, fieldname, form[fieldname].value)

class FeedbackData(FormData):
    fieldnames = ('field1', 'field2', 'field3', 'field4', 'field5')
    def __repr__(self):
        return '%(name)s' % vars(self)


if __name__ == '__main__':
    sys.stderr = sys.stdout
    form = cgi.FieldStorage()
    data = FeedbackData(form)

pickle.dump(data, open('/home/nemir/output/form.txt', 'w'))


print "Content-type: text/html\n\n"
print "<h3><center>You did it!</center></H3>"
sys.exit()



More information about the Python-list mailing list