Solved

Problem passing "hidden field"

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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…

911 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

21 Experts available now in Live!

Get 1:1 Help Now