Dynamic Form

Victor Subervi victorsubervi at gmail.com
Tue Sep 22 12:50:31 EDT 2009


Well it's Web stuff, sure, but it's written in python :) The code follows.
The problem is that I haven't figured out how to tell the program that the
user has entered data and to clear the cache of that data so that it's not
re-entered. How do I do that?
TIA,
Victor

#!/usr/bin/python

import cgitb; cgitb.enable()
import cgi
import sys,os
sys.path.append(os.getcwd())
import MySQLdb
import re

def upload(company, num):
  form = cgi.FieldStorage()
  num = form.getfirst('num', 0)
  company = form.getfirst('company', '')
  flag = form.getfirst('flag', '')
  num = int(num)
  print "Content-Type: text/html"
  print
  print """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<head xmlns="http://www.w3.org/1999/xhtml">
<style type='text/css'>
.text {  font-family: Arial, Helvetica, sans-serif; font-size: 16px;
text-decoration: none; text-align: justify}
</style>
<title>Global Solutions Group</title>
<meta http-equiv="distribution" content="Global" />
<meta http-equiv="robots" content="index all, follow all" />
<meta name="author" content="This web site developed by beno. You may reach
him at his web site [beno.vi], or by dialing 340-773-0687 and asking for
room 102." />
<body>
"""
  if flag == 'yes':
    host = 'hose'
    db = 'db'
    user = 'user'
    passwd = 'pass'
    database = MySQLdb.connect(host, user, passwd, db)
    cursor= database.cursor()
    pat = '[.,()\[\]\\+=!#$%^&;:"\'*]'
    company = re.sub(pat, '', company)
    sql = 'Category varchar(100), Item varchar(20), Description
varchar(255), UOM varchar(20), Price float(7,2)'
    cursor.execute('create table if not exists %s (%s);' % (company, sql))
    i = 0
    while i < num:
      print i
      cat = 'cat' + str(i)
      cat = form.getfirst(cat, '')
      item = 'item' + str(i)
      item = form.getfirst(item, '')
      descr = 'descr' + str(i)
      descr = form.getfirst(descr, '')
      uom = 'uom' + str(i)
      uom = form.getfirst(uom, '')
      price = 'price' + str(i)
      price = form.getfirst(price, '')
      sql = 'insert into %s (Category, Item, Description, UOM, Price) values
("%s", "%s", "%s", "%s", "%s");' % (company, cat, item, descr, uom, price)
      cursor.execute(sql)
      i += 1
    cursor.close()
    print '<h2>Data entered successfully.</h2>'
    company = ''
    num = 0
  print '<form method="post" action="upload.py">\n'
  print "Company Name: <input type='text' value='%s' size='20'
maxlength='100' name='company' /><br /><br />\n" % (company)
  if num == 0:
      print "How many rows of products shall we include? <input type='text'
value='' size='10' maxlength='10' name='num' /><br />\n"
  else:
    print "<input type='hidden' name='flag' value='yes' />\n"
    print "<input type='hidden' name='num' value='%s' />\n" % (num)
    i = 0
    while i < num:
      print '<hr />\n'
      print "Category: <input type='text' value='' size='20' maxlength='100'
name='cat%s' /><br />\n" % (str(i))
      print "Item: <input type='text' value='' size='20' maxlength='20'
name='item%s' /><br />\n" % (str(i))
      print "Description: <input type='text' value='' size='20'
maxlength='255' name='descr%s' /><br />\n" % (str(i))
      print "UOM: <input type='text' value='' size='20' maxlength='20'
name='uom%s' /><br />\n" % (str(i))
      print "Price: <input type='text' value='' size='10' maxlength='10'
name='price%s' /><br />\n" % (str(i))
      i += 1
  print """
<hr />
<div align='center'>
  <input type='submit' value=' Send ' />
</div>
</form>
</body></html>
"""

upload(company='', num=0)


On Tue, Sep 22, 2009 at 11:34 AM, Simon Forman <sajmikins at gmail.com> wrote:

> On Tue, Sep 22, 2009 at 10:46 AM, Victor Subervi
> <victorsubervi at gmail.com> wrote:
> > Hi;
> > I have a dynamic form in which I do the following:
> > 1) Request two fields (company name, number of entries). That is sent
> back
> > to the form.
> > 2) If the two fields are not None, the form requests other data. That,
> too,
> > is sent back to the form.
> > 3) That new data is then entered into a MySQL table.
> > The problem is, that when I go back to refresh the form, the data is
> > re-entered into the table! How do I prevent that?
> > TIA,
> > Victor
> >
>
> First, this seems like it's not a python question, rather it's seems
> to be about some web stuff.
>
> Second, there's not enough information to tell you anything useful.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090922/e68d4244/attachment-0001.html>


More information about the Python-list mailing list