6 Replies Latest reply on Mar 31, 2011 12:44 PM by Dan Bracuk

    Form Calculation Question

    djkhalif Level 1

      Hi all,

       

      I have a question regarding form calculation, I am building a CF Purchase Requisition form and I need to calculate totals immediately. I've tried binding but, cannot figure out the calculations. Has anyone done this before and can provide help?

       

      <cfinput name="qty"/> <cfinput name="unitPrice"/> <cfinput name="subTotal" value=""/> subTotal should be (form.qty*form.unitPrice). It's not real time.

       

       

      djkhalif

        • 1. Re: Form Calculation Question
          Amiya Padhi Level 1

          Can you please let me know where you are trying to do the calculation? Is not it in the form action?

          • 2. Re: Form Calculation Question
            djkhalif Level 1

            I want to perform the calculation before form action, as user enters values the calculations should be performed.

            • 3. Re: Form Calculation Question
              Reed Powell Level 3

              You will need to put onChange calls into the INPUT tags so that as values are changed then the JavaScript function that they call does the recalculation of the appropriate cells.  You probably only need one function that all of them will call.  There are plenty of examples of doing the JS calculations all over the web - it's common on any of the Intro to JS sites.  Google will be your friend.

               

              Here is an example:

              <script language="javascript">
              function updateTotals()

                document.PO.TotalCost.value=parseFloat('0'+document.PO.PrintingCost.value)+parseFloat('0' +document.PO.PaperCost.value)+parseFloat('0'+document.PO.BindingCost.value)+parseFloat('0' +document.PO.MailingCost.value);


              }
              </script>

               

               

              <cfinput type="text" name="PrintingCost" value="#numberFormat(getPO.PrintingCost,"9.99")#" size="5" onChange="updateTotals();">

              1 person found this helpful
              • 4. Re: Form Calculation Question
                djkhalif Level 1

                Reed,

                 

                Thanks. I'll give it a shot.

                 

                djkhalif

                • 5. Re: Form Calculation Question
                  Amiya Padhi Level 1

                  I guess it will be a better idea to validate the type of the data before perfoming the before we do any calculation. Because in the numeric fiels the user may enter alplhabets. Then your javascript calculation will fail. To do this you can use CF client side validations or you can also add explicit codes to valide the data in javascript

                  • 6. Re: Form Calculation Question
                    Dan Bracuk Level 5

                    In addition to verifying that you are not receiving unwanted characters, you have to contend with empty fields.  A hint from one of my co-workers,

                     

                    // subtracting zero from each value forces it to be an integer rather than a string, even if it is empty

                     

                    Or, you can do what I did once and pre-populate every field with 0, and make it 0 if the user changes it to something that is not a number.

                    1 person found this helpful