Solved

Problem passing "hidden field"

Posted on 2014-10-12
4
138 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

772 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