# calculating total with discount

• 12-07-2012, 12:15 PM
kagamushu
calculating total with discount
Hello,
i have a simple form that calculates the total of some checkboxes which works fine. Now, I need to offer a flat rate when 2 or more boxes are checked.
Example: 1 box = 100, 2 boxes or more = 175.
Finally, certain checkboxes must be exempt from the discount.
I am new to JS and just to get the form i currently have to work, took me forever, so please be gentle!
Here is my code:

<script type="text/javascript">
function calculate() {
var elems = document.forms['form1'].elements;
var total = 0;
for(var i=0;i<elems.length;i++) {
if (elems[i].checked) {total += +(elems[i].value);}
}
elems['total'].value = total;
}
</script>

boxes where 2 or more must = 175:

<input type="checkbox" name="checkbox_group_1[]" onclick="calculate()" value="100" title="Check here" />
<input type="checkbox" name="checkbox_group_2[]" onclick="calculate()" value="100" title="Check here" />
<input type="checkbox" name="checkbox_group_3[]" onclick="calculate()" value="100" title="Check here" />

exempt boxes:

<input type="checkbox" name="checkbox_group_4[]" onclick="calculate()" value="20" title="Check here" />
<input type="checkbox" name="checkbox_group_5[]" onclick="calculate()" value="20" title="Check here" />

thanks!
• 12-07-2012, 02:29 PM
WestWeb
Here's a new function calculate that will change total's value to whatever you want if two checkboxes get checked:
Code:

```function calculate() {     var elems = document.forms['form1'].elements;     var total = 0;     var c = 0     for(var i=0;i<elems.length;i++)     {         if (elems[i].checked)         {             c++;             // if both flat rate boxes are checked, apply your flat rate value to total             if(c >= 2)             {                 total = 175; // whatever your flat rate is             }             // else just apply checkbox values to total             else             {                 total += +(elems[i].value);             }         }     }     elems['total'].value = total; }```
Also, notice how much more readable the code is now that I've nested everything. ;)
• 12-10-2012, 10:21 AM
kagamushu
hey,
thanks that works great.
How about exempting certain boxes from the flat rate? In other words, certain checked boxes would always add to the total and be exempt from the function.
thanks again!
• 12-11-2012, 07:01 PM
WestWeb
Not tested, but I think you'll have to add id's to uniquely identify the input's you don't want to include, like:
Code:

`<input type="checkbox" id="cb1" name="checkbox_group_1[]" onclick="calculate()" value="100" title="Check here" />`
Then just change your if statement, that looks for 2 or more checkboxes, to check that the inputs with id's are NOT checked, like:
Code:

```if(c >= 2 && (!document.getElementById('cb1').checked) ) {     total = 175; // whatever your flat rate is } // else just apply checkbox values to total else {     total += +(elems[i].value); }```
• 12-19-2012, 03:02 PM
kagamushu
hey thanks,
it didn't work for some reason, so i removed the check boxes for now.
thanks though
• 12-19-2012, 03:11 PM
WestWeb
No problem: but, in the future, don't be afraid to show your code or anything like that. I may have been able to spot where you went wrong and it could be working. Feel free to do so if you want to get it working still.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.08163 seconds
• Memory Usage 2,360KB
• Queries Executed 11 (?)
Template Usage (20):
• (3)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates