View Full Version : Calculating price based on form field quantity

07-11-2006, 06:23 PM
I'm trying to update a price based on a form field where the quantity can be entered. The total calculated price must also be passed to a PayPal button on the same page.

Looking through this forum, I've come across this script:

<script type="text/javascript">
function getVal(){
var doc = document['oFrm'];
doc['tot'].value = ((doc['id1'].value)*12.95);
<form name="oFrm">
<input name="id1" type="text" onkeyup="javascript:getVal();" onkeydown="javascript:getVal();" />
<input name="tot" type="text" />

My problem is that the first item is $12.95, but succeeding items are $10.95 each, so 1 item is $12.95, but 2 items are $23.90.
You can see the page this is for at http://www.aventine-hill.com/order.html

Any help would be appreciated.

07-11-2006, 07:10 PM
I would advise against doing this in pure client script.

I mean what if they have script turned off? or what if the user typed:

javascript:window.document.forms[0].elements['tot'].value = "0";

into the url bar?

and got everything for free?

07-11-2006, 07:24 PM
<script language="javascript">

function calcTotal(){
for(i=0; i<document.forms['myform'].length; i++){
if ((document.forms['myform'].elements[i].type=="text") && (document.forms['myform'].elements[i].name.substring(0,3)=="qty")) {
currtotal = currtotal + Number((document.forms['myform'].elements[i].value*document.forms['myform'].elements[++i].value));




<form name="myform">
<input type="text" name="qty0"> Item 1 <input type="hidden" name="price0" value="10.95"><br>
<input type="text" name="qty1"> Item 2 <input type="hidden" name="price1" value="12.95"><br>

Total: <input type="text" name="total">
<input type="button" onclick="calcTotal();">



07-11-2006, 07:25 PM
Good point, but the site is selling Flash Cards for people who want to become teachers, so I don't think they'll have the knowledge to do this. Also, the total has to be passed to Paypal, which generates an email to the seller. The $0.00 price would be a clue that there's a problem.

02-08-2009, 05:04 PM
arnyinc, I've just tried using your code and so far it's the only that works but I've run into a snag. When I enter a certain quantity for my product, it comes up weird.

For example, I'm only selling one product at 2.99.
I can type in 1 in the quantity field and the price displays fine.
But if I enter 5, 9, 10, 13, 17, 18, 20, 21 and 23 and possibly other numbers, it appears as 68.77000000000001.

Anyway I can fix this and also is there anyway I can display the currency symbol in the total price box?


Philip M
02-08-2009, 05:51 PM
totalPrice = "$" + totalPrice.toFixed(2); // totalPrice is now a string value
I do urge you to consider the advice given by VR2.

You have two problems:-

a) The site will only work if Javascript is enabled
b) The user can manipulate the price as indicated, not just to 0 but to 0.10, 0.99 etc.

It is naive to believe that people training to be teachers will not have the knowledge and/or inclination to do this.

JavaScript form validation only provides convenience for users, not security. If for example a script verifies that the user agreed to a firm's terms of service, or filters invalid characters out of fields that should only contain numbers, or performs a price x quanity calculation, the validation must also be performed server-side, and not just on the client.

02-08-2009, 05:54 PM
Ahh thank you.
Is there anything you'd recommend?

Joe :)