These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

event.value on multiple form fields - result vanishes

CUB_ger
Registered: Aug 4 2011
Posts: 4
Answered

Hi,
after searching hours - can't find the bug.
 
I have multiple fields calculating a hectare value out of the input value.
Therefor i have:
 
- One field hectare total
- Multiple fields you can type in a percentage - the result will be in hectare.
 
Each of these multiple fields is formatted with 2 decimal places (Format tab). Following script is appliad to each field:
 
// get field values
var a1 = this.getField("totalHa").value;
var a2 = this.getField("currentFieldName".value;
 
// get percentage for current field
var result1 = a1 / 100 * b1;
 
// decimal correction for math.floor
decResult1 = Math.floor (result1 * 100);
 
// get the correct decimal back and put it into the field
event.value = (decResult1/100);
 
---------------
 
Now... this all works perfect - but only for one field. I used this code for every field but changed the variable number "1" to "2"...(only to eliminate errors...)
 
BUT: When i put a value in field one it will show correct. When i put a value in field two the value in field one vanishes and the correct value of field two appears... and so on. Is there a problem with event.value for multiple fields? Is there an alternative for populating a field with the result?
 
Kind regards
 
Christoph Busch

My Product Information:
Acrobat Pro 9.0, Macintosh
maxwyss
Registered: Jul 25 2006
Posts: 255
Normally, independent Calculate events don't affect each other. So, there may be dependencies between the fields which are not quite obvious.

What you can do, is consolidate all your calculations into one single script, and you may assign that script even to a hidden field with no connection at all to the other fields. In fact, this would also speed up your form, and, of course, it gives you much more control over the calculations.

Hope this can help.

Max Wyss.

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
From your description it sounds like you are performing calculations in the same fields that are used as inputs? Is this correct? If so then you have big problems, the calculate event is called each time any field on the form is changed, so your script is continually called and run on whatever the current value is in the field. You also run the risk of creating an infinite loop of events. I'm surprised you haven't crashed Acrobat.

The solution is to separate the input fields from the result fields.

Did you check the console window to see if any errors were reported?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

CUB_ger
Registered: Aug 4 2011
Posts: 4
Hi,
thx for your suggestions.

@thomp
Yes - i do calculate the input value and place the result within the same field. There are some errors at the console - will get back with it on monday.
(I didn't have a problem with acrobat crashes on this document up to now - maybe luck...)

@all
I will try to seperate the calculation either to the document level or to an extra field like suggested from you and come back with an anwer on monday.

Have a nice weekend

Christoph
CUB_ger
Registered: Aug 4 2011
Posts: 4
Hi,
now i tried to source out the calculation to a function (appliad to a blind field):


function calc1() {

// get field values
var a1 = this.getField("totalHa").value;
var a2 = this.getField("field1").value;

// get percentage for current field
var result1 = a1 / 100 * b1;

// decimal correction for math.floor
var decResult1 = Math.floor (result1 * 100);

// get the correct decimal back
var finalResult = (decResult1/100);

// Return the result to field1
return finalResult;
}

I called this function with: calc1(); within the field1.

...But nothing happens.

I tried:

1. instaed of:
return finalResult;
event.value = final
But than the error is the same like in post 1: the result vanishes from field one and is appliad to field 2 when i insert a value to field two.

2. to write the result into field one (within the function):
getField("field1").value = finalResult;
But nothing happens to field one.

How can i write the result of the function back into field one without event.value?

---------------

Thanks for your help.

Christoph
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
Was there anything displayed in the console window?

The problem (as explained in my first post) is that the script in the calculation event uses on it's own field data as input. It doesn't matter if you move the script into a function, as long as it's operating on it's own value you have a problem.

So let me explain how this works in detail. All calculation scripts in all fields on the form are triggered when any field on the form is changed.
1. You click on field1 and type data into field1
2. You hit enter or tab out of field,
3. This action commits the entered field data to the field value
4. if the commit action changes data in field1, then
5. The Calculation scripts for all fields on the form are triggered
6. The calculation on field1 proceeds as expected with the new data and displays the result

7. You click on another field and enter data.
8. Again, this action triggers all the calculation scripts on the form
9. But this time the field1 calculation uses the result from the last calculation as input. Which displays an unexpected result.


There is a way to do this. But it is an advanced scripting solution. Your best solution is to use separate fields for input and the calculation result.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

CUB_ger
Registered: Aug 4 2011
Posts: 4
Hi thomp,
thank you for your extended explenation (i was completely misguided). Now i understand it (and it works)... this explaines some odd results one time in progress as well.

PS: no errors in console

Kind regards
Christoph