...

View Full Version : If..else..if..else for percentage discount, dependent on total



AnnaKaren
09-28-2011, 07:31 AM
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.

Old Pedant
09-28-2011, 08:03 AM
No code, no help.

Show what you have so far.

AnnaKaren
09-28-2011, 08:25 AM
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

Philip M
09-28-2011, 09:25 AM
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. :o 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

Old Pedant
09-29-2011, 01:32 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum