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 to the CF scene
    Join Date
    Apr 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multiple if else statements

    I am trying to set up a booking form for a conference.
    The organisers want to offer discount where for every 5 places booked the sixth one is free.

    I have tried to write the script that will calculate the total taking this discount into account. It works when I write if(el.value >= 6) total = (el.value * 40) -40; for the first if statement
    but when I change it to if(el.value >= 6 and el.value <=11) total = (el.value * 40) -40;
    the form stops working.
    I am aware I need to add further 'else if' statements for bigger group bookings but I would first like to get this basic one working.

    I am sure there is a 'prettier' mathematical formula for doing this calculation which if anyone can helpwith I would much appreciate it!

    Here is my complete script so far...

    <script language="javascript" type="text/javascript">
    function updateCost() {

    var total = 0;
    var cf = "totalCost";


    var el = document.getElementById('delegates');
    if(el.value >= 6 and el.value <=11) total = (el.value * 40) -40; else total = el.value * 40;



    document.getElementById('totaloutput').innerHTML = "&pound;" + total;
    document.getElementById('totalprice').value = total;

    }
    </script>

    <link href="style.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <form method="post" action="https://www....">

    <input type="hidden" name="redirect" value="http://www..../thanks.htm" />
    <input type="hidden" name="totalprice" id="totalprice" value="0" />

    <table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">

    <tr bgcolor="#ECD399">

    <td width="672"><span class="style3"><strong>Total Number of conference places </strong></span></td>
    <td width="250" colspan="2"><input type="text" name="delegates" id="delegates" value="0" onchange="updateCost();" />

    </tr>

    <tr bgcolor="#ECD399">

    <td><span class="style3">Total cost:</span></td>
    <td id="totaloutput">&pound;0.00 </td>

    <td id="totaloutput"><input type="button" name="Button" value="Calculate" /></td>
    </tr>
    <tr bgcolor="#ECD399">
    <td colspan="3"><input type="submit" name="Submit" value="Submit" />
    &nbsp;&nbsp;
    <input type="reset" name="Reset" value="Reset" /></td>
    </tr>

    </table>


    </form>

  • #2
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    if(el.value >= 6 and el.value <=11) total = (el.value * 40) -40;
    if(el.value >= 6 && el.value <=11) total = (el.value * 40) -40;

    Kinda lazy right now, it might be

  • #3
    New Coder
    Join Date
    Nov 2003
    Location
    San Francisco
    Posts
    45
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Code:
    if(el.value >= 6 && el.value <=11){
    total = el.value * 40 - 40
    }else{
    total = el.value * 40
    }
    This should do. -CC

  • #4
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Thank you,

    That did the trick!

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Can't you simply do:
    Code:
    total = 40 * (el.value - Math.floor(el.value / 6));
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #6
    New Coder
    Join Date
    Nov 2003
    Location
    San Francisco
    Posts
    45
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Good alternative method though the first solution allows him to easily change his discount to any value he likes. -CC

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    "The organisers want to offer discount where for every 5 places booked the sixth one is free."

    So surely Dumpfi's code is correct, allowing for 1, 2, 3, 4, etc. free places if 6, 12, 18, 24 etc. places booked.


  •  

    Posting Permissions

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