Solved

Problem passing "hidden field"

Posted on 2014-10-12
4
134 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
Comment Utility
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 82

Expert Comment

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

Author Closing Comment

by:jameskane
Comment Utility
Thanks !!

 says me with a red face !!
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now