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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Someone help please

    wondering if someone can help me with a script , probably will be easy for you, ive been fumbling through it for about 5 hours and i cant figure out what to do, basciall I have 2 drop down menus, one for and item and one for quantity, the script multiplies the item value by the quantity then totals it, I have been trying to add a "Shipping" dropdown in there, but when I put the code in, The Item value, multiplies by the quantiy selected, then multiplies by the shipping amount, I need it to just add the shipping to the subtotal, and for the life of me I just cannot do it, please help! Thanks in advance. This is what i have so far sorry its a little length -
    Last edited by cythrill; 05-24-2012 at 10:49 PM.

  • #2
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I shortend the script - if somone can help please

    I cannot get the Product times quantity plus shipping to work - please help

    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <script>
    function doit(sel,qty,sub)
    {
    var grand = 0;
    var temp = sel.options[sel.selectedIndex].value
    var temp2 = document.frm.elements[qty].options[document.frm.elements[qty].selectedIndex].value
    var temp3 = document.frm.elements[shipping].options[document.frm.elements[shipping].selectedIndex].value
    document.frm.elements[sub].value = temp*temp2
    for(i=0; i<3; i++) //change i <3 according to the number of sub total boxes u have. if u have 6 sub total change it to i <5
    {
    grand += document.frm.elements['sub'+i].value*1;
    }
    document.frm.grand.value = grand;
    }

    function doit1(qty,sel,sub)
    {
    var grand =0;
    var temp = qty.options[qty.selectedIndex].value
    var temp2 = document.frm.elements[sel].options[document.frm.elements[sel].selectedIndex].value
    var temp3 = document.frm.elements[shipping].options[document.frm.elements[shipping].selectedIndex].value
    document.frm.elements[sub].value = temp*temp2
    for(i=0; i<3; i++)//change i <3 according to the number of sub total boxes u have. if u have 6 sub total change it to i <5
    {
    grand += document.frm.elements['sub'+i].value*1;
    }
    document.frm.grand.value = grand;
    }

    var grand =0;
    var temp = shipping.options[shipping.selectedIndex].value
    var temp2 = document.frm.elements[sel].options[document.frm.elements[sel].selectedIndex].value
    var temp3 = document.frm.elements[qty].options[document.frm.elements[qty].selectedIndex].value
    document.frm.elements[sub].value = temp+temp2
    for(i=0; i<3; i++)//change i <3 according to the number of sub total boxes u have. if u have 6 sub total change it to i <5
    {
    grand += document.frm.elements['sub'+i].value*1;
    }
    document.frm.grand.value = grand;
    }

    </script>
    </HEAD>

    <BODY>


    <form name = "frm">
    <select name = "sel" id = "sel" onchange = "doit(this,'qty','sub0')">
    <option value="19" >#1006 Deco Daisy - $19.00</option>
    <option value="19" >#1007 South Beach - $19.00</option>
    <option value="19" >#1008 Book Berry - $19.00</option>
    <option value="19" >#1009 Love Story - $19.00</option>
    </select>
    <select name = "qty" id = "qty" onchange = "doit1(this,'sel','sub0')">
    <option value="1" >1</option>
    <option value="2" >2</option>
    <option value="3" >3</option>
    </select>
    <select name = "shipping" id = "Shipping" onchange = "doit(this,'qty','sub1')">
    <option value="1.95" >$1.95</option>
    <option value="5.95" >$5.95</option>
    <option value="8.95" >$8.95</option>
    <option value="10.95" >$10.95</option>
    </select>
    <input type = "text" readonly name = "sub0">
    <br>
    <select name = "sel1" id = "sel1" onchange = "doit(this,'qty1','sub1')">
    <option value = "0">select here</option>
    <option value="" >Select Product</option>
    <option value="19" >#1006 Deco Daisy - $19.00</option>
    <option value="19" >#1007 South Beach - $19.00</option>
    <option value="19" >#1008 Book Berry - $19.00</option>
    <option value="19" >#1009 Love Story - $19.00</option>
    </select>
    <select name = "qty1" id = "qty1" onchange = "doit1(this,'sel1','sub1')">
    <option value="1" >1</option>
    <option value="2" >2</option>
    <option value="3" >3</option>
    </select>
    <input type = "text" readonly name = "sub1" >
    <br>
    <select name = "sel2" id = "sel2" onchange = "doit(this,'qty2','sub2')">
    <option value = "0">select here</option>
    <option value="" >Select Product</option>
    <option value="19" >#1006 Deco Daisy - $19.00</option>
    <option value="19" >#1007 South Beach - $19.00</option>
    <option value="19" >#1008 Book Berry - $19.00</option>
    <option value="19" >#1009 Love Story - $19.00</option>
    </select>
    <select name = "qty2" id = "qty2" onchange = "doit1(this,'sel2','sub2')">
    <option value="1" >1</option>
    <option value="2" >2</option>
    <option value="3" >3</option>
    <option value="4" >4</option>
    </select>
    <input type = "text" readonly name = "sub2">
    <br>
    Grand Total:<input type = "text" readonly name = "grand">
    </form>

    </BODY>
    </HTML>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Code:
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <script type="text/javascript">
    function doit()
    {
        var form = document.frm;
        var grand = 0;
        for ( pnum = 1; pnum < 999999; ++pnum )
        {
            var prod = form["product"+pnum];
            if ( prod == null ) break; // out of the for loop when no more products
            prod = Number(prod.value);
            var qty  = Number(form["qty"+pnum].value);
            var ship = Number(form["shipping"+pnum].value);
    
            var sub = prod * qty + ship;
            form["sub"+pnum].value = sub.toFixed(2);
            grand += sub;
        }
        frm.grand.value = grand.toFixed(2);
    }
    
    
    </script>
    </HEAD>
    
    <BODY>
    
    
    <form name = "frm">
    <select name = "product1" onchange = "doit()">
        <option value="0">--choose a product--</option>
        <option value="19" >#1006 Deco Daisy - $19.00</option>
        <option value="19" >#1007 South Beach - $19.00</option>
        <option value="19" >#1008 Book Berry - $19.00</option>
        <option value="19" >#1009 Love Story - $19.00</option>
    </select>
    <select name = "qty1" onchange = "doit()">
        <option value="1" >1</option>
        <option value="2" >2</option>
         <option value="3" >3</option>
    </select>
    <select name = "shipping1" onchange = "doit()">
        <option value="1.95" >$1.95</option>
        <option value="5.95" >$5.95</option>
        <option value="8.95" >$8.95</option>
        <option value="10.95" >$10.95</option>
    </select>
    <input type = "text" readonly name = "sub1">
    <br>
    <select name = "product2" onchange = "doit()">
        <option value="0">--choose a product--</option>
        <option value="19" >#1006 Deco Daisy - $19.00</option>
        <option value="19" >#1007 South Beach - $19.00</option>
        <option value="19" >#1008 Book Berry - $19.00</option>
        <option value="19" >#1009 Love Story - $19.00</option>
    </select>
    <select name = "qty2" onchange = "doit()">
        <option value="1" >1</option>
        <option value="2" >2</option>
         <option value="3" >3</option>
    </select>
    <select name = "shipping2" onchange = "doit()">
        <option value="1.95" >$1.95</option>
        <option value="5.95" >$5.95</option>
        <option value="8.95" >$8.95</option>
        <option value="10.95" >$10.95</option>
    </select>
    <input type = "text" readonly name = "sub2">
    <br>
    <select name = "product3" onchange = "doit()">
        <option value="0">--choose a product--</option>
        <option value="19" >#1006 Deco Daisy - $19.00</option>
        <option value="19" >#1007 South Beach - $19.00</option>
        <option value="19" >#1008 Book Berry - $19.00</option>
        <option value="19" >#1009 Love Story - $19.00</option>
    </select>
    <select name = "qty3" onchange = "doit()">
        <option value="1" >1</option>
        <option value="2" >2</option>
         <option value="3" >3</option>
    </select>
    <select name = "shipping3" onchange = "doit()">
        <option value="1.95" >$1.95</option>
        <option value="5.95" >$5.95</option>
        <option value="8.95" >$8.95</option>
        <option value="10.95" >$10.95</option>
    </select>
    <input type = "text" readonly name = "sub3">
    <br>
    <br>
    Grand Total:<input type = "text" readonly name = "grand">
    </form>
    
    </BODY>
    </HTML>
    Notice that the code automatically adjusts for any number of products.

    Normally, shipping is added PER ORDER, not per item. But I left it as you had it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    iBall's answer is correct, of course, but I suspect it may be too advanced for you to follow.

    If so, it's easy to change my code to get rid of the bogus shipping costs:

    Just change this one line:
    Code:
            var sub = prod * qty + ship;
    to this:
    Code:
            var sub = prod * qty;
            if ( sub == 0 ) sub += ship;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Naturally, that should have been != 0 instead of == 0.

    Just a typo.

    iBall could have pointed that out but of course he prefers to make a big deal over little things.

    Nothing ever changes, does it Bullant?

    Don't you lie awake nights trying to remember which person you are supposed to be tomorrow, Mishu?

    Wouldn't you *PLEASE* put me back on your ignore list? It was much quieter then.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    The great advantage of being on iBall's ignore list is that, although many of his posts simply repeat what has already been said, he cannot seize on something in one of my posts to start a pointless and belligerent argument, or sneer at an accidental typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

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