?
Solved

Python : How to use variables with HTML FORM Hidden Fields.

Posted on 2015-02-22
11
Medium Priority
?
773 Views
Last Modified: 2015-02-23
I have a HTML 5 form with two hidden fields. I need to assign the values of existing variables to each hidden field as shown below in. The form has a cgi action page where I recover the values of the variables and other field info which has been directly entered into the form.

Everything works, except the hidden fields. The variable names do not get translated into their numerical values - so all the form transmits is OrderID and TotalCost. I have tried using print(OrderID) and print(TotalCost) but that does no work.

Is here a special syntax to be used in html forms to address this  ?

Thanks for any help


EXTRACT FROM HTML FORM
print("<input name='OrderID' type='hidden' id='OrderID' value='OrderID' />")
      print("<input name='TotalCost' type='hidden' id='TotalCost' value='TotalCost' />")

Open in new window


ACTION PAGE
def getData():
      CustomerID = formData.getvalue('CustomerID')
      DelDate = formData.getvalue('DelDate')
      DelTime = formData.getvalue('DelTime')
      [b]TotalCost = formData.getvalue('TotalCost')
      OrderID =  formData.getvalue('OrderID')[/b]
      return CustomerID,DelDate,DelTime,TotalCost,OrderID

Open in new window

0
Comment
Question by:jameskane
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 40624467
You need to have your print statement actually output/include the values of your variables:

OrderID = '13313131'  # OrderID is a defined variable with the value you want in it
print("<input name='OrderID' type='hidden' id='OrderID' value='{0}' />".format(OrderID))

Open in new window


The above will output:
<input name='OrderID' type='hidden' id='OrderID' value='13313131' />

Open in new window

0
 

Author Comment

by:jameskane
ID: 40624479
OrderID = '13313131'......  I don't know how to get that statement.  I do have OrderID which has a value, say 13313131.. but to see what its actual value is at the moment I only know print(OrderID) ??
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 2000 total points
ID: 40624493
I'm just trying to illustrate that OrderID has to be a defined at the point that you're trying to output it.   Because without seeing your code, I'm guessing there's a good chance it's not going to be.  

Anyway... a string's format method allows you to embed variables within it.   See http://infohost.nmt.edu/tcc/help/pubs/python/web/new-str-format.html for a detailed explanation and sample.

Basically you need to get a string to include the variable you want in it.  Under newer versions of python (2.6+) that's typically down with the format method (https://docs.python.org/2/library/string.html#format-string-syntax).  

But you could also use concatenation (+):

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

Or you could use the % operator:

print("<input name='OrderID' type='hidden' id='OrderID' value='%s' />" % OrderID)

Or you could use a string template (https://docs.python.org/2/library/string.html#template-strings).

Use format, just showing the others as examples.  The idea is all the same.  You need to get a string that includes the value of your variable.  The above methods are ways to do that.
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40624498
0
 

Author Comment

by:jameskane
ID: 40624679
Thanks very much clockwatcher - I spent a day trying to figure this out !!

The OrderID hidden field worked perfectly first time !!

Have a problem still with the TotalCost. The form statement is as follows:

print("<input name='TotalCost' type='hidden' id='TotalCost' value='{0}' />".format(GrandTotalCost))

TotalCost is the name of the table column I want to update. However, the name of the variable with the value is called GrandTotalCost.   Other than that, the conditions are exactly the same as those which exist for OrderID.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40624689
In your original post above, the value returned is just 'TotalCost', not 'GrandTotalCost'.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 40624890
Are you getting an error?  Is it outputting a value different than what you're expecting it to?  What exactly is the problem that you're having with TotalCost?   We'll probably need a bit more to go on to be of much help.
0
 

Author Comment

by:jameskane
ID: 40625251
TotalCost is being picked up as [12] - i.e. as a list. This is then fed into the database update function (below) which uploads a 0, instead of 12 (as in this example.

def UpdateCustomerOrderInfo(db,cursor):
      cursor.execute ("""
         UPDATE customerorder
         SET CustomerID=%s, DelDate=%s, DelTime=%s, TotalCost=%s
         WHERE OrderID=%s
      """, (CustomerID, DelDate, DelTime, TotalCost, OrderID))
      cursor.close ()
      db.commit()
      db.close()  

Open in new window


I have tried to change TotalCost before the above function is executed .... TotalCost=TotalCost[12] - but this does not return 12 - rather [ ???  Bizzare ???  and the database update function gets updated in column TotalCost from NULL to 0.
0
 

Author Comment

by:jameskane
ID: 40625255
sorry, error above... should read TotalCost=TotalCost[0]
0
 

Author Comment

by:jameskane
ID: 40625361
Been looking at this again clockwatcher -  the problem might be the value of GrandTotalCost I am feeding into the hidden field. It may be bracketed []. I need to leave it for the moment. Please assume the problem has been solved and I will get back later to confirm.
Thanks very much
0
 

Author Closing Comment

by:jameskane
ID: 40625398
Thanks very much for the solution and the great pointers.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 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