Calculating Percentage in a form

• 03-23-2010, 01:50 AM
keco86
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>                 <input type="text" id="delivery" size="10" maxlength="10" readonly="readonly" value="0" />         <br />                 <label for="storageCosts">Storage Costs:</label>                 <input type="text" id="storageFees" size="10" maxlength="10" readonly="readonly" value="0" />         <br />         <label for="discount">Discount:</label>                 <input type="text" id="discount" size="10" maxlength="10" readonly="readonly" value="0" />         <br />                 <label for="taxes">Taxes:</label>                 <input type="text" id="roundedTaxes" size="10" maxlength="10" readonly="readonly" value="0" />         <br />                 <label for="total">Final Total:</label>                 <input type="text" id="finalTotal" size="10" maxlength="10" readonly="readonly" value="0" />         </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>```
• 03-23-2010, 05:19 AM
tirna
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.
• 03-23-2010, 07:46 AM
criterion9
To be more specific you have an assignment operator (=) instead of a comparison operator (==). This same problem occurs in several places.

Quote:

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.
• 03-23-2010, 11:19 AM
keco86
Thank-you criterion9 & tirna I actually dreamed that exact thing last night...worked like a charm! Much obliged.

X vBulletin 4.2.2 Debug Information

• Page Generation 0.12864 seconds
• Memory Usage 2,397KB
• Queries Executed 11 (?)
Template Usage (22):
• (1)bbcode_code_printable
• (1)bbcode_html_printable
• (1)bbcode_quote_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
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates