Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Calculating price based on form field quantity

    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);
    }
    //-->
    </script>
    <form name="oFrm">
    <input name="id1" type="text" onkeyup="javascript:getVal();" onkeydown="javascript:getVal();" />
    <input name="tot" type="text" />
    </form>

    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.

  • #2
    VR2
    VR2 is offline
    New Coder
    Join Date
    Jul 2006
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by VR2; 07-11-2006 at 06:15 PM.

  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Code:
    <html>
    <head>
    <script language="javascript">
    
    function calcTotal(){
      currtotal=0
      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));
        }
      }
      document.forms['myform'].elements['total'].value=currtotal;
    }
    
    </script>
    
    </head>
    
    <body>
    
    <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>
    
    <br>
    Total: <input type="text" name="total">
    <br>
    <input type="button" onclick="calcTotal();">
    </form>
    
    </body>
    
    </html>

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    Thanks.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    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.
    Last edited by Philip M; 02-08-2009 at 04:57 PM.

  • #7
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahh thank you.
    Is there anything you'd recommend?

    Joe


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •