SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru htown's Avatar
    Join Date
    Mar 2005
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with calculations (check box value)

    I have been on this board for a few days now and have received a lot of help so I am calling on you guys once again.

    I have a form at http://www.txmover.com/pro/quotesheet.html and it all works just fine. If you look in the form you will see four check boxes in the "Living Room / Hall" Section and three check boxes in the "Dinning Room" section.

    If you have a look at the source code you will see at the top that I am adding up all of these items, but I also want to ad in an extra cost for the check boxes. Lets just say they will equal 5. If some one checks this box 5 needs to be added to the total. The CHECKBOX is already set to a VALUE="1" and I thought that would be enough but the calculations fails.

    I have temporarily removed the check boxes from the calculations so you can see how it is suppose to work.

    So my question is how to I assign a value to the checkbox that the can be put into the calculation. Why does "1" not work?

    Thanks
    Houston Brown
    Split Light Designs
    Bringing your ideas to light.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This replacement for your "cu" function is a bit more robust. It won't break if there are elements missing, and I think it handles the checkboxes the way you want (not sure).

    Code:
    function cu(p_name) {
    	// get the elements
    	var element = document.forms['quotesheet'].elements[p_name];
    	var element_m = document.forms['quotesheet'].elements['m'+p_name];
    	
    	// default values for the number of items and the weight thing
    	var v_item = 0;
    	var v_wt = 1;
    	
    	// default value for the result
    	var result = 0;
    	
    	// check that the element exists
    	if( element ) {
    		if( !isNaN(parseInt(element.value)) )
    			v_item = parseInt(element.value);
    		
    		// check that the "m" element exists (if not, the value stays at 1)
    		if( element_m ) {
    			if( !isNaN(parseInt(element_m.value)) )
    				v_wt = parseInt(element_m.value);
    		}
    		
    		// check if this element is a checkbox, if it is and it's not checked, set the value to zero
    		if( element.getAttribute("type") == "checkbox" ) {
    			if( !element.checked ) {
    				v_item = 0;
    			}
    		}
    	
    		// calculate the result
    		result = v_item * v_wt;
    		
    	}
    				
    	return result;
    
    }
    PS it's spelled DINING

  3. #3
    SitePoint Guru htown's Avatar
    Join Date
    Mar 2005
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really appreciate you taking that kind of time to help out. It seems to make that calculation change, I have not seen it do that before. Although the code makes a lot more sense as far as efficiency is concerned. The problem now is that the value of the check box (which happens to be the amount being charged for the service) can not be displayed. That is the purpose of all of the other fields having the hidden fields with the letter
    "m" in front of the NAME.

    There also still seems to be a calculation error. I have given the value of 100 to plasma30. When I fill out the form and press calculate I get a total on the next page. If I go back and check the box for PLASMA on the 30" or less item I would expect the number to increase by 100. It only goes up 40 and I am not sure why but I can work on that later I guess.

    Hope this is making sence.

    Thanks
    Houston Brown
    Split Light Designs
    Bringing your ideas to light.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There might be problems on the page that processes the form (the coldfusion page...I don't know coldfusion). The best thing to do would be put some alerts in the javascript and see if the total comes out as expected before the form submits.

  5. #5
    SitePoint Guru htown's Avatar
    Join Date
    Mar 2005
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do have some alerts on the .html page (javascrip) and they are commented out at this time. If I uncomment them the page just goes to the cold fusion page and I never see the ECHO of the javascript. I am not sure how to go about doing that.
    Houston Brown
    Split Light Designs
    Bringing your ideas to light.

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For testing add a new button that calls the same function that is called when submitting the form but don't have it submit.

  7. #7
    SitePoint Guru htown's Avatar
    Join Date
    Mar 2005
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do not have it submit it doesn’t show up as a button. The actual Submit button I have now does not call any function:

    It looks like this:
    Code:
          .
          .
          .
          </table>
    	  <input type="submit" name="Add" value="Calculate" />
    	  <input name="Total" type="hidden" id="Total">
        </form>
    Sorry, I am not knowledgeable about this. Probably need a little more hand holding.

    Thanks
    Houston Brown
    Split Light Designs
    Bringing your ideas to light.

  8. #8
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, sorry, the function calc is called by submitting the form (onsubmit).

    Add a button for testing underneath your submit button something like this:
    <input type="button" value="testing" onclick="calc();" />

    That you can use to test the calc function (with alerts n stuff)

  9. #9
    SitePoint Guru htown's Avatar
    Join Date
    Mar 2005
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, that worked great. I have it all working now.

    I have just one more question if you do not mind. Now that I have it all working, I need to pass a different value for when one of the check boxes (or all for that matter) are checked other than the actual assigned number value.

    For example, if someone checks 30" TV PLASMA the value for that is 10. So that I can indicate on the email that my client and the visitor gets that the tv is indeed a plasma I DO NOT Want to put the value there. I would rather put the letter "P".

    I tried adding some JavaScript myself:

    if (plasma30 = 10) p30 = "P"

    and then on the email it would read:
    30" TV or Less - P

    or if it wasn’t a Plasma TV then:
    30" TV or Less - NP (for Not Plasma or it could just be blank but NP would be better)

    This make sense?
    Houston Brown
    Split Light Designs
    Bringing your ideas to light.

  10. #10
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the javascript handling the email part? It might be simpler to have that dealt with by coldfusion.

  11. #11
    SitePoint Guru htown's Avatar
    Join Date
    Mar 2005
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the javascript is only doing the calculations and then the totaas and varaibles are being passed to the CFMAIL tag.

    What I need to know is how to assign the word "PLASMA" to a variable if a chackbox is checked. (TRUE)

    Thanks
    Houston Brown
    Split Light Designs
    Bringing your ideas to light.


Bookmarks

Posting Permissions

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