Skip to main content
Known Participant
July 3, 2010
Question

Forms - adding two fields (how?)

  • July 3, 2010
  • 2 replies
  • 3011 views

I have been programming in CF for a long time but for some reason, I haven't been asked to do this..  I have a form that has two fields - Food Total and Drink Total..  when the user enters the Price of total food and Total drink, have the total automatically fill in the Input field of Alltotal.

This is done on a Form.  I don't want them to press submit to show total.  I need it to show up automatically.

This topic has been closed for replies.

2 replies

Inspiring
July 3, 2010

You have to use javascript.  I had a similar job once, but with more fields.  Here are the steps.

When you load your page, put 0 in both inputs.

Write a js function that gets called in the onChange for each input.  The first thing to do is verify that you have a valid number with no more than two decimal places.  If the user puts in anything wrong, put 0 in the entire form field.

Then write another function that runs afterwards.  It will put the sum of the two fields into the 3rd one.  Make sure that field is disabled so the user can't overwrite it.

Hint, don't rely on NaN or parseFloat() for the first function.

Known Participant
July 5, 2010

Guys:  thanks for your help.  This java script worked - KINDA but it does not ADD columns.  The multiplication part works, but when I put the ADD symbol, it joins (binds) the two fields.  I think it has something to do with a TEXT field verse numeric.

All I did was change the x*y  to x+y

----------------------------------------
          
   <cfajaxproxy bind="javascript:doMultiply({first},{second})" />
<script>
function doMultiply(x,y) {
document.getElementById("result").value = x+y;
}
</script>

<form>
<input id="first" size="3"> X <input id="second" size="3"> = <input id="result">

</form>

Inspiring
July 5, 2010

What happens when input 1 is two and a half bucks and,

you accidentally type a comma instead of a period for your decimal point,

or

you put a dollar sign in front of if?

existdissolve
Inspiring
July 3, 2010

This one's easy.

You should check the docs: ColdFusion has stuff built into it that will make this short work.

And more importantly, Google is your friend.  Searching for something like "coldfusion bind form fields" brings up a billion results that specify exactly how to do this.

Known Participant
July 3, 2010

Thanks... You see, the problem was I didn't know the term "Bind".  I googled everything but

didn't realize that was the expression.  It works, at least auto populates the total field with both variables. In other words, I put in 10.00 and 30. and it shows 10.0030.   I need to figure out how to sum this with bind.

Known Participant
July 3, 2010

OK... I've read about 40 pages and cannot find how to ADD the BIND statements of two fields to equal Total.

Any help would be Greatly appreciated...