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

Sum # of Fields then Multiply Dollar Amount to get Total

Dezign
Registered: Dec 5 2011
Posts: 6
Answered

I have a project that requires a registration form that allows the attendees to type their names into individual text field boxes. I need an invoice form at the end of the registration form that will automatically calculate the number of attendees then multiply the total number of attendees by $25 and then have a total at the bottom of the invoice.
 
So, if I I have 3 text field boxes, with three names entered, I want to multiply 3 x $25 and come up with the total at the bottom of the invoice. Note: There may be more than 3 names.
 
The text field boxes are on page two of the registration form and the invoice is page 3.
 
I am new to using Acrobat Professional and Javascript.
 
Could someone help me create this invoice?
 
Thanks.
 

My Product Information:
Acrobat Pro 9.4.3, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
The most important element when implementing this is to have consistent names for the text fields. Let's say they are named Attendee1, Attendee2, Attendee3, etc.

You can then use a script like this as the custom calculation script of your total field (set the field as read-only, and the format as Number, with the dollar currency symbol):

  1. var pricePerPerson = 25;
  2. var numAttendees = 0;
  3. var i = 1;
  4. while (true) {
  5. var f = this.getField("Attendee"+i);
  6. if (f==null) break;
  7. if (f.value!="")
  8. numAttendees++;
  9. i++;
  10. }
  11. event.value = pricePerPerson * numAttendees;
Now, if you want to make it a bit more advanced and save the price per person in a form field (instead of hard-coding it), then replace the first line of the script above with this (of course, the field name can be adjusted):
  1. var pricePerPerson = this.getField("PricePerPerson").value;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Dezign
Registered: Dec 5 2011
Posts: 6
Okay thanks, but just to be sure my question was understood, (since I am so unfamiliar with this calculations operation)...

On page 2 I have text field boxes for each below:

Name of Guest 1
Name of Guest 2
Name of Guest 3
Name of Guest 4
Name of Guest 5
Name of Guest 6
Name of Guest 7
Name of Guest 8

The person filling out the form will enter a name in each text box for attendees.

I want the invoice to automatically sum up in a Field box the total number of names entered (if 3 or 8) and then the Total field box at the bottom of the invoice will be the Total Charge for the number of guests multiplied by $25 .

So, if the registration form has 4 guest name entered, then the invoice shows a field that contains the number 4 and then the total at the bottom of the invoice shows $100.

Hopefully, that makes better sense! If the above code will make that work, thanks so much! If not, could you please help me create the invoice?

Any help will be greatly appreciated!

Thanks.







try67
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
Try it yourself... just change this line:
var f = this.getField("Attendee"+i);
to this:
var f = this.getField("Name of Guest "+i);

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Dezign
Registered: Dec 5 2011
Posts: 6
Okay, in the total field at the bottom of the invoice

The invoice is not calculating a total, it is only showing 0.00. Here is what I have input into the Custom Calculate Script box after double clicking on my total field box: I did set it to read only and number with currency symbol.

var pricePerPerson = 25;
var numAttendees = 0;
var i = 1;
while (true) {
var f = this.getField("Name of Guest"+i);
if (f==null) break;
if (f.value!="")
numAttendees++;
i++;
}
event.value = pricePerPerson * numAttendees;

I saved the form and then typed one name into 4 of the fields "name of guest 1", "name of guest 2", etc.

The form is not totalling the number of guests that were entered into each field.



try67
Expert
Registered: Oct 30 2008
Posts: 2398
I think you removed the extra space at the end of the name of the field (after "Guest"), which I placed in my previous post.

By the way, you should change the preference to show the JS console on errors and warnings. It provides very valuable information about what is going wrong with the scripts in the file.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Dezign
Registered: Dec 5 2011
Posts: 6
I am sorry but I do not understand ....

The field boxes are labled


Name of Guest 1
Name of Guest 2
Name of Guest 3 and so on

the script I entered is:

var pricePerPerson = 25;
var numAttendees = 0;
var i = 1;
while (true) {
var f = this.getField("Name of Guest"+i);
if (f==null) break;
if (f.value!="")
numAttendees++;
i++;
}
event.value = pricePerPerson * numAttendees;

Do I need to insert a space ? If so, where?

and the field that I am using for my total value is named "Total"...should it be named "event.value"?

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Yes, it needs to be this (notice the space after "Guest"!):

var f = this.getField("Name of Guest "+i);

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Dezign
Registered: Dec 5 2011
Posts: 6
Well, I added the space after Guest but it still did not work. I also changed the name of the "total" field to "event.value" and that did not work either. I saved the form on both changes, and had already added three names in three of the Name of Guest fields. Still no luck getting a total dollar amount to show up in the "total" field.


Any other suggestions?

try67
Expert
Registered: Oct 30 2008
Posts: 2398
There's no need to change the name of the "total" field. "event.value" will work regardless of the field's name.
If you want, send me the file in question and I'll check what's going wrong. It's probably something very minor.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Dezign
Registered: Dec 5 2011
Posts: 6
Okay, will do sending to

try6767 [at] gmail [dot] comWatch for email from whenelephantsfly01 [at] yahoo [dot] comAlso, can you advise on some good tutorials for beginning forms, calculations as well as database collection of data?

Thanks.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Update for those interested: the problem was that the fields names were in upper-case.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com