• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Form Calculation Question

Participant ,
Mar 31, 2011 Mar 31, 2011

Copy link to clipboard

Copied

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

TOPICS
Advanced techniques

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 31, 2011 Mar 31, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 31, 2011 Mar 31, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 31, 2011 Mar 31, 2011

Copy link to clipboard

Copied

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();">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 31, 2011 Mar 31, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 31, 2011 Mar 31, 2011

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 31, 2011 Mar 31, 2011

Copy link to clipboard

Copied

Reed,

Thanks. I'll give it a shot.

djkhalif

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation