www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: quantity/discount calculator PLEASE HELP!!

  1. #1
    Join Date
    Oct 2009
    Posts
    17

    quantity/discount calculator PLEASE HELP!!

    im writing a script for a calculator that gives a 15% percent discount for every child enrolled greater than 1. IM LOST! Here's what i have so far. once i find a way to calculate the discount, i can adjust the grandtotal display. Thanks!


    <script language="JavaScript">
    <!--
    function CalculateTotals(){
    f=document.orderform;
    f.total1.value=parseInt(f.qty1.value)*225;
    f.total2.value=parseInt(f.qty2.value)*200;
    f.total3.value=parseInt(f.qty3.value)*100;

    f.grandtotal.value=parseInt(f.total1.value)
    +parseInt(f.total2.value)
    +parseInt(f.total3.value);}

    //-->
    </script>

  2. #2
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    Not sure what you are looking for here.

    Try this:
    PHP Code:
    var price1 = (child_count * <price_per_child>);
      if (
    child_count 1) { price1 -= ((price1 * <discount_percent>) / 100); } 
    Replace <price_per_child> with something for the price, and <discount_percent> as percentage without &#37; (15% = 15)

    PS: Javascript is NOT secure for setting prices for electronic commerce. Make the server deal with prices, so the client cannot manipulate them.
    Computer programmer / web developer.

  3. #3
    Join Date
    Oct 2009
    Posts
    9
    Hey it might be the way ur accessing your form, I got this code from http://www.w3schools.com/jsref/coll_form_elements.asp it might help:

    Code:
    <html>
    <body>
    
    <form id="myForm">
    Firstname: <input id="fname" type="text" value="Mickey" />
    Lastname: <input id="lname" type="text" value="Mouse" />
    <input id="sub" type="button" value="Submit" />
    </form>
    
    <p>Get the value of all the elements in the form:<br />
    <script type="text/javascript">
    var x=document.getElementById("myForm");
    for (var i=0;i<x.length;i++)
      {
      document.write(x.elements[i].value);
      document.write("<br />");
      }
    </script>
    </p>
    
    </body>
    </html>

  4. #4
    Join Date
    Oct 2009
    Posts
    17
    im need it to calculate the total number of students enrolled and apply a 15% discount for each student over 1.
    nothing im trying is working.

  5. #5
    Join Date
    Oct 2009
    Posts
    9
    maybe the related form might give me a better idea of what ur trying to do

  6. #6
    Join Date
    Oct 2009
    Posts
    17
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <script language="JavaScript">
    <!--
    function CalculateTotals(){
    f=document.orderform;
    f.total1.value=parseInt(f.qty1.value)*225;
    f.total2.value=parseInt(f.qty2.value)*200;
    f.total3.value=parseInt(f.qty3.value)*100;

    f.grandtotal.value=parseInt(f.total1.value)
    +parseInt(f.total2.value)
    +parseInt(f.total3.value);}
    //-->
    </script>

    <body>

    <div id="calculator">
    <form name="orderform" method="post" action="formprocessor">
    <table border="3">
    <tr><td>
    <input name="qty1" size="3" OnBlur="CalculateTotals()" /></td>
    <td>Newborn to 1 year old</td>
    <td align="right">$225</td>
    <td><input name="total1" size="7"
    OnFocus="document.orderform.qty2.select();
    document.orderform.qty2.focus();" /></td></tr>
    <tr><td>
    <input name="qty2" size="3" OnBlur="CalculateTotals()" /></td>
    <td>2 to 5 year old</td>
    <td align="right">$200</td>
    <td><input name="total2" size="7"
    OnFocus="document.orderform.qty3.select();
    document.orderform.qty3.focus();" /></td></tr>
    <tr><td>
    <input name="qty3" size="3" OnBlur="CalculateTotals()" /></td>
    <td>5 to 10 year old</td>
    <td align="right">$100</td>
    <td><input name="total3" size="7"
    OnFocus="document.orderform.qty4.select();
    document.orderform.qty4.focus();" /></td></tr>
    <tr><td></td><td></td>
    <td align="right"><b>&nbsp;TOTAL:</b></td>
    <td><input name="grandtotal" size="7"
    OnFocus="document.orderform.qty1.select();
    document.orderform.qty1.focus();" /></td></tr>
    </table>

    <br /></form>
    <script language="JavaScript">
    <!--
    f=document.orderform;
    f.qty1.value=0; f.qty2.value=0;
    f.qty3.value=0;
    f.total1.value=0; f.total2.value=0;
    f.total3.value=0;
    //-->
    </script>
    </div>

    </body>

    </html>

  7. #7
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    I think you are looking for something like this:
    PHP Code:
    <script language="Javascript" type="text/javascript">
    <!--
    function 
    CalculateTotals(){
    var 
    total 0;
    var 
    item_prices = new Array();
    var 
    counts = new Array();
    document.orderform;
    counts[0] = parseInt(f.qty1.value);
    counts[1] = parseInt(f.qty2.value);
    counts[2] = parseInt(f.qty3.value);
    item_prices[0] = (counts[0] * 225);
    item_prices[1] = (counts[1] * 200);
    item_prices[2] = (counts[2] * 100);

    item_prices[1] -= ((item_prices[1] * 15) / 100);
    item_prices[2] -= ((item_prices[2] * 15) / 100);

    total = (item_prices[0] + item_prices[1] + item_prices[2]);

    f.total1.value item_prices[0];
    f.total2.value item_prices[1];
    f.total3.value item_prices[2];
    f.grandtotal.value parseInt(f.total1.value) + parseInt(f.total2.value) + parseInt(f.total3.value);}
    //-->
    </script> 




    PS: It might be better to make an array to house the prices etc., then generate the HTML for the form and output it. If you intend actual finicial processing, do not use Javascript to set the price. Use it perhaps for displaying the price to the user, but do not accept any price from the client machine, only the order. You could use PHP or whatever technology you are using to output the javascript array, so you can update prices with client estimations while maintaining the server's integrity.
    Last edited by savvykms; 10-29-2009 at 06:57 PM.
    Computer programmer / web developer.

  8. #8
    Join Date
    Oct 2009
    Posts
    17
    i replaced my script with yours but it still doesnt calculate the discount

  9. #9
    Join Date
    Oct 2009
    Posts
    9
    could it be that this may work:

    Code:
    f.total1.value=(parseInt(f.qty1.value)*225)-((parseInt(f.qty1.value)*225)*0.15);

  10. #10
    Join Date
    Oct 2009
    Posts
    17
    still getting the same values

  11. #11
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    I just tried it out in IE8, it works just fine:
    HTML Code:
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    
    <script language="javascript" type="text/javascript"> 
    <!-- 
    function CalculateTotals(){ 
    var total = 0; 
    var item_prices = new Array(); 
    var counts = new Array(); 
    f = document.orderform; 
    counts[0] = parseInt(f.qty1.value); 
    counts[1] = parseInt(f.qty2.value); 
    counts[2] = parseInt(f.qty3.value); 
    item_prices[0] = (counts[0] * 225); 
    item_prices[1] = (counts[1] * 200); 
    item_prices[2] = (counts[2] * 100); 
    
    item_prices[1] -= ((item_prices[1] * 15) / 100); 
    item_prices[2] -= ((item_prices[2] * 15) / 100); 
    
    total = (item_prices[0] + item_prices[1] + item_prices[2]); 
    
    f.total1.value = item_prices[0]; 
    f.total2.value = item_prices[1]; 
    f.total3.value = item_prices[2]; 
    f.grandtotal.value = parseInt(f.total1.value) + parseInt(f.total2.value) + parseInt(f.total3.value);} 
    //--> 
    </script>
    <body>
    
    <div id="calculator">
    <form name="orderform" method="post" action="formprocessor">
    <table border="3">
    <tr>
    <td><input name="qty1" size="3" onblur="CalculateTotals()" /></td>
    <td>Newborn to 1 year old</td>
    <td align="right">$225</td>
    <td><input name="total1" size="7" onfocus="document.orderform.qty2.select(); document.orderform.qty2.focus();" disabled="disabled" /></td></tr>
    <tr>
    <td><input name="qty2" size="3" onblur="CalculateTotals()" /></td>
    <td>2 to 5 year old</td>
    <td align="right">$200</td>
    <td><input name="total2" size="7" onfocus="document.orderform.qty3.select(); document.orderform.qty3.focus();" disabled="disabled" /></td></tr>
    <tr>
    <td><input name="qty3" size="3" onblur="CalculateTotals()" /></td>
    <td>5 to 10 year old</td>
    <td align="right">$100</td>
    <td><input name="total3" size="7" onfocus="document.orderform.qty4.select(); document.orderform.qty4.focus();" disabled="disabled" /></td></tr>
    <tr><td></td><td></td>
    <td align="right"><b>&nbsp;TOTAL:</b></td>
    <td><input name="grandtotal" size="7" onfocus="document.orderform.qty1.select(); document.orderform.qty1.focus();" disabled="disabled" /></td></tr>
    </table>
    
    <br />
    </form>
    <script language="javascript" type="text/javascript">
    <!--
    f=document.orderform;
    f.qty1.value = 0;
    f.qty2.value = 0;
    f.qty3.value = 0; 
    f.total1.value = 0;
    f.total2.value = 0;
    f.total3.value = 0; 
    //-->
    </script>
    </div>
    </body>
    </html>
    Computer programmer / web developer.

  12. #12
    Join Date
    Oct 2009
    Posts
    9
    with the code I posted, if u had 1 student for each group the total would be &#163;491.

    your initial code gave a total of &#163;525 if u had 1 student for each group. hence you can't be getting the same value.

  13. #13
    Join Date
    Oct 2009
    Posts
    17
    youre right. with this code:

    <script language="JavaScript">
    <!--
    function CalculateTotals(){
    f=document.orderform;
    f.total1.value=parseInt(f.qty1.value)*225;
    f.total2.value=parseInt(f.qty2.value)*200;
    f.total3.value=parseInt(f.qty3.value)*100;
    f.total1.value=(parseInt(f.qty1.value)*225)-((parseInt(f.qty1.value)*225)*0.15);
    f.grandtotal.value=parseInt(f.total1.value)
    +parseInt(f.total2.value)
    +parseInt(f.total3.value);}

    //-->
    </script>

    it is calculating the discount. the only this is i need it to apply the discount only for more than one child.

  14. #14
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    total quantity > 1 = 15&#37; discount?
    Computer programmer / web developer.

  15. #15
    Join Date
    Oct 2009
    Posts
    17
    where would i put that?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles