PDA

View Full Version : multiple if else statements



Joerubes
01-13-2007, 04:09 PM
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>

rubenbuhr
01-13-2007, 04:30 PM
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

ccemmett
01-13-2007, 04:53 PM
if(el.value >= 6 && el.value <=11){
total = el.value * 40 - 40
}else{
total = el.value * 40
}


This should do. -CC

Joerubes
01-13-2007, 04:58 PM
Thank you,

That did the trick!

dumpfi
01-13-2007, 05:17 PM
Can't you simply do:


total = 40 * (el.value - Math.floor(el.value / 6));
dumpfi

ccemmett
01-13-2007, 05:26 PM
Good alternative method though the first solution allows him to easily change his discount to any value he likes. -CC

Philip M
01-13-2007, 06:10 PM
"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.