www.webdeveloper.com
Results 1 to 4 of 4

Thread: Calculating Percentage in a form

  1. #1
    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% 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>

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    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. #3
    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. #4
    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.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13862 seconds
  • Memory Usage 2,887KB
  • Queries Executed 13 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)bbcode_html
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (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
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.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 (71):
  • 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_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • 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
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_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
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates