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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question If..else..if..else for percentage discount, dependent on total

    I have my cumulative totals box at the very end of my order form and I'm required to have another underneath that one that will give a particular discount depending on how great the total of the order is.

    Would someone please be able to give me the javascript codes that will allow me to do this and also tell me where I'm to put them.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,168
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    No code, no help.

    Show what you have so far.
    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.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, sorry.

    <script language="JavaScript" type="text/javascript">


    function CalculateTotal(frm) {
    var order_total = 0


    for (var i=0; i < frm.elements.length; ++i) {


    form_field = frm.elements[i]


    form_name = form_field.name

    if (form_name.substring(0,3) == "TOT") {

    prev = frm.elements[i-1]
    prev_name = prev.name


    item_price = parseFloat(prev_name.substring(prev_name.lastIndexOf("_") + 1))


    item_quantity = parseInt(prev.value)

    // Update the order total
    if (item_quantity >= 0) {
    item_total = item_quantity * item_price
    form_field.value = item_total
    order_total += item_total

    }
    }
    }

    // Display the total rounded to two decimal places
    frm.GRANDTOTAL.value = order_total
    }


    </script>



    //and then the last row with the total is:

    <tr>
    <td></td>
    <td></td>
    <td></td>
    <td>Total</td>
    <td><input type="text" name=GRANDTOTAL size="10" onFocus="this.form.elements[0].focus()"></td>

    </tr>

    //and I need another separate row that'll discount the above total

  • #4
    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
    Quote Originally Posted by AnnaKaren View Post
    Would someone please be able to give me the javascript codes that will allow me to do this and also tell me where I'm to put them.
    //and I need another separate row that'll discount the above total
    Sorry, but we will not do your homework for you. It is not really in your best interests that others do all or most of your homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase.

    Show your attempt at coding for this, and then doubtless someone will be willing to correct/improve it.

    // Display the total rounded to two decimal places
    frm.GRANDTOTAL.value = order_total

    How does that round to 2 decimal places?

    <script language="javascript"> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 09-28-2011 at 10:59 AM.

    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,168
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    I really wanted to see your HTML.

    From that JS, I can deduce some of the HTML, but not all of it.

    And I'm not convinced that you are approaching the problem in the easiest/best way, in any case.
    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.


  •  

    Posting Permissions

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