# Thread: Calculating Percentage in a form

1. Registered User
Join Date
Oct 2008
Location
Beautiful BC
Posts
14

## Calculating Percentage in a form

Hi there and thanks in advance for any assistance.
I am writing a form and doing a bunch of calculations one of which is a percentage based on the value selected. I am returning a correct result for two of the values but for the other two I am not.
Clearly I am missing something and maybe not testing for it but as I am clueless I can't think of what.
The 'discount' value is what I am looking for. It should return as 0 for values of 1 but it is doing the math as though it is multiplying by 1. And again for the value of 6 it should be 10&#37; but is taking the value as 4.6 or something.

Code is below I've included all of it (sorry) for better ease of understanding the math that is happening:
HTML Code:
```<form id="form1" action="" method="get">
<fieldset>
<p>
<label for="Type">Type of Storage:</label>
<select id="Type" name="type">
<option name="Heated Storage" value="60">Heated - \$60/month</option>
<option name="Unheated Storage" value="50">Unheated - \$50/month</option>
<option name="Offsite Storage" value="40">Offsite - \$40/month</option>
</select>
</p>
<p>
<label for="Length">How long you need storage:</label>
<select id="Length" name="length">
<option name="Don't know" value="1">Unsure (month to month)</option>
<option name="One month" value="1">One month </option>
<option name="3 months" value="3">3 months (5% pre-payment discount)</option>
<option name="6 months" value="6">6 months (10% pre-payment discount)</option>
<option name="one year" value="12">One year (15% pre-payment discount)</option>
</select>
</p>
<p>
<label for="Gobox">Number of Goboxes:</label>
<select id="Gobox" name="gobox">
<option value="1">1 - \$70 delivery fee</option>
<option value="2">2 - \$100 delivery fee</option>
<option value="3">3 - \$130 delivery fee</option>
<option value="4">4 - \$160 delivery fee</option>
<option value="5">5 - \$190 delivery fee</option>
<option value="6">6 - \$220 delivery fee</option>
<option value="7">7 - \$250 delivery fee</option>
<option value="8">8 - \$280 delivery fee</option>
<option value="9">9 - \$310 delivery fee</option>
<option value="10">10 - \$340 delivery fee</option>
</select>
</p>
<p>
<label for="delivery">Delivery:</label>
<br />
<label for="storageCosts">Storage Costs:</label>
<br />
<label for="discount">Discount:</label>
<br />
<label for="taxes">Taxes:</label>
<br />
<label for="total">Final Total:</label>
</p>

<p align="right" ><input type="button" id="submit" value="Get an Esitmate" onclick="processForm()" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
</fieldset>
</form>

<script type="text/javascript">

function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(100,dec))/Math.pow(100,dec);
return result;
}

function processForm() {
//get form variables
var theType = form1.type.options[form1.type.selectedIndex].value;
var theLength = form1.length.options[form1.length.selectedIndex].value;
var theGoBox = form1.gobox.options[form1.gobox.selectedIndex].value;

//set tax rate
var theTaxes = 0.07;

//set shipping rate
var theShipping = 30.00;

//variable for order total
var orderTotal = 0.00;

//the display box
var deliveryBox = document.getElementById( "delivery" );
var storageBox = document.getElementById( "storageFees" );
var discountBox = document.getElementById( "discount" );
var taxesBox = document.getElementById( "roundedTaxes" );
var totalBox = document.getElementById( "finalTotal" );

//Calculate Delivery
delivery = (theGoBox * theShipping) + 40;

//Calculate Storage Costs
storageFees = ((theType * theGoBox)* theLength);

//Calculate prepayment Discount
if (theLength = 3) {
//multiply storage costs by 5%
discount = storageFees *(5/100);
}
else if (theLength = 6) {
//multiply storage costs by 10%
discount = storageFees *(10/100);
}
else if (theLength = 12) {
//multiply storage costs by 15%
discount = storageFees *(15/100);
}
else {
discount = 0;
}

//Calculate Taxes
taxes = (storageFees + delivery) * theTaxes;
var roundedTaxes = roundNumber(taxes,2);

//figure final total
finalTotal = delivery + storageFees + roundedTaxes;

//output to text boxes
deliveryBox.value = "\$" + delivery;
storageBox.value = "\$" + storageFees;
discountBox.value = "\$" + discount;
taxesBox.value = "\$" + roundedTaxes;
totalBox.value = "\$" + finalTotal;

}

</script>```

2. Had a quick look through your code. I haven't checked the logic but you have a syntax error at

Code:
```
if (theLength = 3) {```
and in similar lines elsewhere in your code.

Fix that and hopefully your form will process correctly.

3. MPM, PMP, CSM, BS CIS
Join Date
Jan 2009
Location
Ohio
Posts
3,356
To be more specific you have an assignment operator (=) instead of a comparison operator (==). This same problem occurs in several places.

I haven't checked the logic but you have a syntax error at
Technically this is not a syntax error. It is a logical error. A syntax error would prevent execution of the code. A logical error doesn't do what was intended.

4. Registered User
Join Date
Oct 2008
Location
Beautiful BC
Posts
14
Thank-you criterion9 & tirna I actually dreamed that exact thing last night...worked like a charm! Much obliged.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

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

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.25264 seconds
• Memory Usage 2,899KB
• Queries Executed 15 (?)
Template Usage (35):
• (1)bbcode_code
• (1)bbcode_html
• (1)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (4)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (4)postbit
• (4)postbit_onlinestatus
• (4)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./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/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (73):
• init_startup
• friendlyurl_resolve_class
• 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_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete