• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

How do I pass an amount variable from within a website page inside javascript to an outside var in the html for posting to paypal?

Hi everyone,

I have some javascript in a webpage which calculates the total amount of selections on the page.  I need to somehow pass this variable "TotalAmt" to the html area of the page to be then posted to paypal.  The problem is that once the javascript block ends so does the variable etc.

Can anyone help?

Please let me express my thanks in advance.

Best regards,

  • 3
  • 2
1 Solution
got some code?
i dont know if this will work since i have no context but:

// this is the html form box that contains the value to be posted
<input type="text" id="totalamt" name="totalamt">

// this is the javascript that goes *directly under* the line (in the javascript) where TotalAmt is finally calculated
  document.forms[0]..value = TotalAmt;
sorry that should read:

 document.forms[0].totalamt.value = TotalAmt;
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Not sure how a javascript question ended up in the C# area, but can't you just use something like this:

var totalAmount;

function calculateAmount()
    totalAmount = Math.random();

Depends on how you want to pass the variable on.  If you'll be using Javascript (eg: XMLHttpRequest), this approach will be fine.  If you'll be using HTTP POST, I'd suggest using a hidden input control (ie: <input type="hidden" id="totalAmount" />) and using document.getElementById('totalAmount').value = ... to set the value.
DynotoeAuthor Commented:
Hi Gents,

Boethius,  I implemented what you suggested and wrote the following to see if it was working and it is.

document.getElementById('totalamount').value = grandtotal;
var text=document.getElementById('totalamount').value;

}//end of javascript

However I can't seem to get the value into the paypal amount line in the html area further down on the page for posting...

<input type="hidden" name="amount" value="">

where this would normally work...

<input type="hidden" name="amount" value="400.00">

Please Help!!  :)

Best Dynotoe
I'm not totally sure that I've understood your question, so please forgive me if I'm off target.

You'll need to add an id to the element in order to access it easily from javascript:

<input type="hidden" id="amount" name="amount" />

Once you've called document.getElementById('amount').value = grandtotal;, the value will be set correctly.  The HTML source, however, won't display the changed value.  Essentially, the HTML is used to load the page into it's initial state.  After that, the form and all of it's values are held in memory.  When you submit the page, the value recieved by the server will be correct, but the HTML won't have been altered.

Hope this helps.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now