Solved

Problem passing "hidden field"

Posted on 2014-10-12
4
139 Views
Last Modified: 2014-10-12
I have a simple html form, with one text field to collect the first name of user (firstname). This page is Form_one.html  and its action page is ActionPage.cgi  

ActionPage.cgi presents  a second form which has a text field asking for persons age. It also has a hidden field which saves the persons (firstname), obtained from Form_one.html.  

ActionPage2.cgi  takes the inputs (firstname and age) and prints out the message :-

Hello FIRSTNAME your age is AGE      (eg Hello James your age is 45)  HOWEVER, what I am getting is ...

Hello None your age is 45.

The value of firstname is apparently not being passed through to ActionPage2  !!!!

Hopefully you can spot the mistake I am making !!     Many thanks in advance,    James

The code for each page Form_one.html  ActionPage.cgi and ActionPage2.cgi  is show below.

_______________________________________________________________________________________________
FORM_ONE.HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="actionpage.cgi" method="get" name="getname" target="_self">
<input name="age" type="text" />
<input name="submitname" type="submit" value="submit" />
</form>
</body>
</html>

____________________________________________________________________________________________
ACTIONPAGE.CGI

#!C:\Python34\python.exe
import cgi, cgitb
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>
             </head>
             <body>""")

def htmlTail():
      print("""</body>
             </html>""")
     
     
     
# Create instance of FieldStorage
form = cgi.FieldStorage()
     
# Get data from fields
firstname = form.getvalue('firstname')



def   createForm(firstname):
      print("""<form method="post" action="actionpage2.cgi">
      Please enter your age: <input type="text" name="age" placeholder="eg... 25">
      <br>
      <input type="hidden" name="firstname" value="{0}" />
      <br>
      <input type="submit" name="submitname" value="submit age"/>
      </form>""".format(firstname))
     
#main program
if __name__== "__main__":
      try:
            htmlTop()
            createForm(firstname)
            htmlTail()
      except:
            cgi.print_exception()

____________________________________________________________________________________________________________
ACTIONPAGE2.CGI
#!C:\Python34\python.exe
import cgi, cgitb
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>
             </head>
             <body>""")

def htmlTail():
      print("""</body>
             </html>""")
     
     
     
# Create instance of FieldStorage
def getData():
      formData = cgi.FieldStorage()
      firstname = formData.getvalue('firstname')
      age = formData.getvalue('age')
      return firstname, age




     
#main program
if __name__== "__main__":
      try:
            htmlTop()
            xyz = getData()
            print("Hello {0[0]} your age is {0[1]}".format( xyz))
            htmlTail()
      except:
            cgi.print_exception()
0
Comment
Question by:jameskane
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40375812
In your initial form page, replace
<input name="age" type="text" />

Open in new window

with
<input name="firstname" type="text" />

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40375941
Yes, @Robert Schutt, that made it work on my computer.
0
 

Author Closing Comment

by:jameskane
ID: 40376032
Thanks !!

 says me with a red face !!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40376050
Don't worry about it.  Half my programming time is fixing little things like that.  Especially happens when you copy one file to create another.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
python pyusb how to find a file on mass storage 3 93
Downgrade Python 3.5 to 3.3 30 994
Turning python script into an applet 12 116
Getting the NAO robot to play soccer 1 26
Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question