Link to home
Start Free TrialLog in
Avatar of jameskane
jameskane

asked on

Python : Pre fill form field with value of variable

I have a cgi  page which contains an html5 form . This form should have the top field pre filled with the value of a variable
Delta_euro_total.  The value of this variable is available as you can see from the attached image of the page output. However, only the variable name is being pre filled.

I am attaching the code for the CGI page.  Line 134 starts the function definition Page() which is called later in the code - line 256.

The problem must be with the syntax used in the function.  Hopefully is is obvious to the experienced eye !

Thanks for your help


#!C:\Python34\python.exe
import cgi
#import cgitb
import cgitb
cgitb.enable()
import mysql.connector as conn
import collections
import datetime
def htmlTop():
    print("""Content-type:text/html\n\n
        <!DOCTYPE html>
        <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title> My Server-side template</title>
                <link href="style.css" rel="stylesheet" type="text/css">

                <script>var __adobewebfontsappname__="dreamweaver"</script>
                <script src="http://use.edgefonts.net/source-sans-pro:n2,i2,n3,i3,n4,i4,n6,i6,n7,i7,n9,i9:default;source-serif-pro:n4:default.js" type="text/javascript"></script>
        </head>
        <body>""")

def htmlTail():
    print(""" its done</body>
        </html>""")

def getData():
    formData = cgi.FieldStorage()
    mylist = formData.getlist('mylist')
    return mylist


def connectDB():
    db=conn.connect(host='localhost' ,user='root' ,passwd='844cheminduplan' ,db='office')
    cursor = db.cursor()
    return db, cursor 

def UpdateInfo2(db,cursor):
    cursor.execute ("""
       UPDATE registrations
       SET  T1ytdlatest='{0}', T2ytdlatest='{1}', T3ytdlatest='{2}'
       WHERE memberID={3}
    """.format(mylist[1], mylist[2], mylist[3],mylist[0]))
    db.commit()
    db.close 
    

    
def UpdateInfo1(db,cursor):#see note at bottom of page  on use of mylist_DUPLICATE
    cursor.execute ("""
       UPDATE registrations
       SET  T1ytd='{0}', T2ytd='{1}', T3ytd='{2}'
       WHERE memberID={3}
    """.format(mylist[1], mylist[2], mylist[3],mylist[0]))
    db.commit()
    db.close
    

    
def deleterow(db,cursor):
    cursor.execute(""" 
    DELETE from registrations_copy
    WHERE memberID={0}
    """.format(mylist[0]))
    db.commit()
    db.close

def insertrow(db,cursor):
    cursor.execute("""
    INSERT INTO registrations_copy
    SELECT * 
    FROM registrations
    WHERE memberID={0}
    """.format(mylist[0]))
    db.commit()
    db.close
    
def InsertTransactions(db,cursor):
    strSQL = "insert into transactions(memberID, YTDPurchases, lastpurchase) values ({0}, {1}, {2})".format(memberID, Delta_euro_total, Delta_euro_total )
    cursor.execute(strSQL)
    db.commit()     
    db.close() 
    
#def InsertTransactions(db,cursor):#this works just as well as one above
    #cursor.execute("""
        #INSERT into transactions (memberID)
        #VALUES ('{0}')
        #""".format(memberID))
        ##VALUES memberID = 'memberID'
    #db.commit()
    #db.close
    

    
def UpdateTransactions(db,cursor):
    db,cursor=connectDB()
    cursor.execute ("""
       UPDATE transactions
       SET  YTDPurchases = {0}, lastpurchase = {1}
       WHERE memberID={2}
    """.format(YTDPurchases, Delta_euro_total, memberID))
    db.commit()
    db.close  
    
    
#def Turn_On_Alibe(db,cursor):#used above for turning on the ALPAID_ytd switch
    #db,cursor=connectDB()
    #cursor.execute ("""
       #UPDATE registrations
       #SET  ALPAID_ytd=1
       #WHERE memberID={0}
    #""".format(mylist[0]))
    #db.commit()
    #db.close
    
    
    
    
    
    
    
def transactioncheck(db,cursor):#This checks to see if there exists a record for memberID
    cursor.execute("""SELECT COUNT(*) from transactions where memberID='%s'"""
    %(memberID))
    result=cursor.fetchone()
    return result # see below as to how this is used

def SelectfromDB(db,cursor):
    cursor.execute("""select YTDPurchases from transactions WHERE memberID = "%s" """
    %(memberID)) 
    YTDpurchases=cursor.fetchone()
    return YTDpurchases  

def page():
    Delta_euro_total = eval(form.getvalue('Delta_euro_total'))
    print("<div id='wrapper'> ")
    
    print("<header>")
    print("<h2 id='stargazer'>") 
    print("ABAC APPLICATION")
    print("</h2>")
    print("<nav>")
    print("<ul>")
    print("<li>")
    print("<a href='#'>")
    print("ABOUT")
    print("</a>")
    print("</li>")
    print("<li>")
    print("<a href='#'>")
    print("IMAGES")
    print("</a>")
    print("<li>")
    print("<a href='#'>")
    print("ARTICLES")
    print("</a>")
    print("<li>")
    print("</ul>")
    print("</nav>")
    print("</header>")
    print("<div id='more'>")
    print("<hr>")
    print("</div>")
    print("<div id='content1'>")   
    print("<form id='form1' name='form1' method='post'>")
    print("<p>&nbsp;</p>")
    print("<p>")
    print("<label for='textfield'>Nouvel Ventes:</label>")
    print("<input name='textfield' type='text' autofocus id='textfield' value = 'Delta_euro_total' form='form1'>")
    print("</p>") 
    print("<p>&nbsp;</p>")
    print("<p>")
    print("<label for='textfield2'>Moins Deductions:</label>")
    print("<input name='textfield2' type='text' autofocus id='textfield2' form='form1'>")
    print("</p>")
    
    print("<p>&nbsp;</p>")
    print("<p>Paiement net :-</p>")
    print("<p>&nbsp;</p>")
    print("<p>")
    print("<label for='textfield3'>Especes:</label>")
    print("<input type='text' name='textfield3' id='textfield3'>")
    print("</p>") 
    
    print("<p>&nbsp;</p>")
    print("<p>")
    print("<label for='textfield4'>Cheques:</label>")
    print("<input type='text' name='textfield4' id='textfield4'>")
    print("</p>") 
    
    print("<p>&nbsp;</p>")
    print("<p>")
    print("<input type='submit' name='submit' id='submit' value='Submit'>")
    print("</p>")
    print("<p>&nbsp;</p>")    
    
    print("</form>")
    
    
    
    
    print("</div>")
    
    print('</div>')    



    
#main program
if __name__== "__main__":
    try:
        htmlTop()
        print("hello this is pay receipt")
        db,cursor=connectDB()
        
        form = cgi.FieldStorage()
        Delta_euro_total = eval(form.getvalue('Delta_euro_total'))
        print(Delta_euro_total, "this is delta euro total")
        mylist = eval(form.getvalue('mylist')) #note, must use eval() because it was a STRING, not a list which
        #was passed from the page action_OrderFormTWO.cgi. If you don't do this, you get a weird situation. 
        # If you have mylist = form.getvalue('mylist') and you print out mylist, it looks like the list you expected
        # But in fact it is a STRING. You notice this if you try and print mylist[0] - you will not get the 
        # the first sublist of mylist - all you will get is [. Similar if you try mylist[1]..... etc
        db,cursor=connectDB()
        UpdateInfo1(db,cursor)
        db,cursor=connectDB()
        deleterow(db,cursor)
        db,cursor=connectDB()
        insertrow(db,cursor)
        memberID =mylist[0]
        memberID =eval(mylist[0])# attention !!!!!!!!!!!!!!
        print(memberID, "from memberID=eval(mylist[0])")
        print(memberID, "from memberID=mylist[0]")
        print(mylist, "mylist")
        print(mylist[0], "memberID")
        print(mylist[1], "T1ytdlatest")
        print(mylist[2], "T2ytdlatest")
        print(mylist[3], "T3ytdlatest")
        db,cursor=connectDB()
        result=transactioncheck(db,cursor)
        number_of_rows=result[0]# !!!!!!!!!!!!!!!!!!!NOTE
        print(number_of_rows)
        if number_of_rows == 0:
            db,cursor=connectDB()
            InsertTransactions(db,cursor) #  Need to Insert the first purchase delta as well as row !!!!******
        else:
            YTDPurchases = SelectfromDB(db,cursor)
            print(YTDPurchases, "YTDPurchases")
            print(type(YTDPurchases),"ytdpurchases")
            type(Delta_euro_total)
            print(Delta_euro_total, "Delta_eURO_TOTAL")
            YTDPurchases = YTDPurchases[0] + Delta_euro_total
            print(YTDPurchases, "ytdpurchases")
            UpdateTransactions(db,cursor)
            
        page()
                  
            
            

        
        
        htmlTail()
    except:
        cgi.print_exception()

Open in new window

User generated image
ASKER CERTIFIED SOLUTION
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jameskane
jameskane

ASKER

Thanks Walter !

james