• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 855
  • Last Modified:

Python 3.4 CGI - passing variable via form hidden field

I have a form which is used for selecting activities/courses. I need to pass the identity of the member (memberID) who has completed a selection on this form to the supporting action page.

I am using the following syntax which does not work - trying to insert the variable memberID into the VALUE part of hidden  field as shown below. I can't figure out the syntax. The effort below gets me 'memberID' - rather than the value of memberID  on the supporting action page printout. However, if I put a real number (like 20) in place of memberID - then that is passed to and processed by the action page.

print("""<input type='hidden' name='memberID' id='memberID'  value=' memberID'/>""")

There is no question that a variable value exists for the form - as I am able to print it out.

I am attaching the form page - you will find the hidden field at line 96.

Many thanks for your help.


#!C:\Python34\python.exe
import mysql.connector as conn

import cgi,cgitb
def htmlTop():
      print("""Content-type:text/html\n\n
             <!DOCTYPE html>
             <html lang="fr">
                 <head>
                  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                  <title> My Server-side template</title>
             <style type="text/css">
                      #floatingbutton {
                          top: 200px;
                          width: 100px;
                          right: 800px;
                          display: block;
                          z-index: 9999;
                          position: fixed;
                      }
                  </style>
     
             </head>
             <body>""")





def htmlTail():
      print(""" its done</body>
             </html>""")
      
def connectDB():
      db=conn.connect(host='localhost' ,user='root' ,passwd='844cheminduplan' ,db='office')
      cursor = db.cursor()
      return db, cursor   


      
def numberofrecords(db,cursor):
      sql = ("select * from activities")         
      cursor.execute(sql)
      number_of_rows=len(activities)
      return number_of_rows

def getData():
      form = cgi.FieldStorage()
      memberID = form.getvalue('memberID')
      return memberID
  
def selectactivities(db,cursor):
      sql = ("select * from activities")         
      cursor.execute(sql)
      activities = cursor.fetchall()
      return activities

def activityorderform(activities):
      print("""<form method="post" action="action_OrderFormTWO.cgi">""")
      print("<table border='1'>")
      print("<tr bgcolor=#FCFFDA>")
      print("<th>#</th>")
      print("<th>Activity</th>")
      print("<th>Jour</th>")
      print("<th>Horaire</th>")
      print("<th>Professeur</th>")
      print("<th>Trimesters</th>")
      
      print("</tr>")
      color = 1
      for each in activities:
            if color is 1:
                  print("<tr bgcolor=#BED3BE>")
            else:
                  print("<tr bgcolor=#D0F5CF>")
            print("<td>{0}</td>".format(each[0]))
            print("<td>{0}</td>".format(each[3]))
            print("<td>{0}</td>".format(each[1]))
      
             
            print("<td>{0}</td>".format(each[2]))
            print("<td>{0}</td>".format(each[4]))
            print("""<td>
                        <input type='checkbox' name='selection' id='selection' value='{0}'>T1</input>
                        <input type="checkbox" name='selectionTWO' id='selectionTWO' value='{0}'>T2</input>
                        <input type="checkbox" name='selectionTHREE' id='selectionTHREE' value='{0}'>T3</input>""".format(each[0]))            
            print("</td>")       
            print("")
            print("</tr>")
            if color is 1:
                  color = 0
            else:
                  color = 1

      print("</table>")
      print("""<input type='hidden' name='memberID' id='memberID' value= memberID/>""")
      print("""<input type="submit" name="mysubmitbutton" ID="floatingbutton" value="SUBMIT" />""")
      print("</form>")      
     
#main program
if __name__== "__main__":
      try:
            htmlTop()
            db,cursor=connectDB()
            activities=selectactivities(db,cursor)
            cursor.close()
            activityorderform(activities)
            db,cursor=connectDB()
            memberID = getData()
            print (memberID)
            number_of_rows=numberofrecords(db,cursor)
            print(number_of_rows, "number of rows")
            #cursor.close()
            print("qsjdflskj")
            htmlTail()

      except:
            cgi.print_exception()

Open in new window

0
jameskane
Asked:
jameskane
  • 4
  • 2
1 Solution
 
Walter RitzelSenior Software EngineerCommented:
I made 2 changes: made your memberID variable global and have fixed your hidden field using the .format() method of the string. About the global variable, I did this just to work. You probably need to pass this value as parameter on your functions to be more correct.

#!C:\Python34\python.exe
import mysql.connector as conn

import cgi,cgitb

memberID = None

def htmlTop():
      print("""Content-type:text/html\n\n
             <!DOCTYPE html>
             <html lang="fr">
                 <head>
                  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                  <title> My Server-side template</title>
             <style type="text/css">
                      #floatingbutton {
                          top: 200px;
                          width: 100px;
                          right: 800px;
                          display: block;
                          z-index: 9999;
                          position: fixed;
                      }
                  </style>
     
             </head>
             <body>""")





def htmlTail():
      print(""" its done</body>
             </html>""")
      
def connectDB():
      db=conn.connect(host='localhost' ,user='root' ,passwd='844cheminduplan' ,db='office')
      cursor = db.cursor()
      return db, cursor   


      
def numberofrecords(db,cursor):
      sql = ("select * from activities")         
      cursor.execute(sql)
      number_of_rows=len(activities)
      return number_of_rows

def getData():
      form = cgi.FieldStorage()
      memberID = form.getvalue('memberID')
      return memberID
  
def selectactivities(db,cursor):
      sql = ("select * from activities")         
      cursor.execute(sql)
      activities = cursor.fetchall()
      return activities

def activityorderform(activities):
      print("""<form method="post" action="action_OrderFormTWO.cgi">""")
      print("<table border='1'>")
      print("<tr bgcolor=#FCFFDA>")
      print("<th>#</th>")
      print("<th>Activity</th>")
      print("<th>Jour</th>")
      print("<th>Horaire</th>")
      print("<th>Professeur</th>")
      print("<th>Trimesters</th>")
      
      print("</tr>")
      color = 1
      for each in activities:
            if color is 1:
                  print("<tr bgcolor=#BED3BE>")
            else:
                  print("<tr bgcolor=#D0F5CF>")
            print("<td>{0}</td>".format(each[0]))
            print("<td>{0}</td>".format(each[3]))
            print("<td>{0}</td>".format(each[1]))
      
             
            print("<td>{0}</td>".format(each[2]))
            print("<td>{0}</td>".format(each[4]))
            print("""<td>
                        <input type='checkbox' name='selection' id='selection' value='{0}'>T1</input>
                        <input type="checkbox" name='selectionTWO' id='selectionTWO' value='{0}'>T2</input>
                        <input type="checkbox" name='selectionTHREE' id='selectionTHREE' value='{0}'>T3</input>""".format(each[0]))            
            print("</td>")       
            print("")
            print("</tr>")
            if color is 1:
                  color = 0
            else:
                  color = 1

      print("</table>")
      print("""<input type='hidden' name='memberID' id='memberID' value='{0}' />""".format(memberID))
      print("""<input type="submit" name="mysubmitbutton" ID="floatingbutton" value="SUBMIT" />""")
      print("</form>")      
     
#main program
if __name__== "__main__":
      try:
            htmlTop()
            db,cursor=connectDB()
            activities=selectactivities(db,cursor)
            cursor.close()
            activityorderform(activities)
            db,cursor=connectDB()
            memberID = getData()
            print (memberID)
            number_of_rows=numberofrecords(db,cursor)
            print(number_of_rows, "number of rows")
            #cursor.close()
            print("qsjdflskj")
            htmlTail()

      except:
            cgi.print_exception()

Open in new window

0
 
jameskaneAuthor Commented:
Ah, see what you have done on the hidden input line. Should have figured that one out!!

I think I understand the basic concept of global variable, but it's not clear to me why this is an issue ?  After all - memberID value is available on the form page and has been printed out - so the function getData() must have worked ? ( see memberIDinform attached.

I note line 6 memberID = None - but don't understand that.  

The action page is returning memberID as  'none'  - see attached memberIDProb image. So the form page has not passed the value of memberID via the 'hidden' input

Sorry - must be mis-understanding something.

AND thanks for taking the time on this.

James



Output from action page showing MemberID as NonememberID value available in form page - printed from line 113
0
 
Walter RitzelSenior Software EngineerCommented:
So, the concept of global variable is a variable that will be available from any place in your code.
That is different from a local variable, which is declared or first assigned inside a function. The local variable exists only on the context of that function. So, if you assign a value to a variable inside a function and try to use it in another function, that will not work. But if the variable is global, it will work.

But on your situation, I made a mistake. You dont need line 6 on your script, you can remove it.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jameskaneAuthor Commented:
error message on running the page
Took away line six, but not get an error message when launching the page. See attached.
0
 
jameskaneAuthor Commented:
AH !   Problem solved.

I find that the problem is due to not declaring memberID before running the function activityorderform(activities) -
which needs to have that in order to work.  So, just moved     memberID = getData() up from line 112 and all is well. Now your hidden input code works !

Thanks very much again for the solution !

Code fix image
0
 
jameskaneAuthor Commented:
Thanks very much !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now