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
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?
Copy link to clipboard
Copied
I want to perform the calculation before form action, as user enters values the calculations should be performed.
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();">
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Reed,
Thanks. I'll give it a shot.
djkhalif