Skip to main content
sw777
Known Participant
March 19, 2017
Answered

Calculating a total based on checkboxes and numeric fields

  • March 19, 2017
  • 1 reply
  • 1749 views

Hi all.

I'm trying to display a "TotalFee" field that needs to return the fees for sitting some exams plus some optional tutorial sessions.

I have two checkboxes called "Exam1" and "Exam2". Candidates can select either or both of these checkboxes.

For each selected exam, a fee of $90 needs to be added to the total (so $180 if both boxes are checked). I'm thinking I need to use two hidden numeric fields with a preset value of 90 in each, and then refer to this field if the associated checkbox is selected (unless there is an easier way to do this).

There is also a numeric field called "TutorialSessions" that allows the candidate to enter the number of tutorial support sessions they want to book into before they sit either exam. This is an optional field.

If a number is entered in the "TutorialSessions" field, it needs to be multipled by 90 to obtain the total cost of tutorial support which is then displayed in a "TutorialFee" field.

Finally, the "TotalFee" field needs to sum the total cost for the selected exams plus the tutorial fee.

Any help would be greatly appreciated.

This topic has been closed for replies.
Correct answer try67

You don't need hidden text fields.

This code should do the trick. Use it as the custom calculation script of "TotalFee":

var totalFee = 0;

if (this.getField("Exam1").value!="Off") totalFee+=90;

if (this.getField("Exam2").value!="Off") totalFee+=90;

totalFee+=Number(this.getField("TutorialFee").value)*90;

event.value = totalFee;

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 20, 2017

You don't need hidden text fields.

This code should do the trick. Use it as the custom calculation script of "TotalFee":

var totalFee = 0;

if (this.getField("Exam1").value!="Off") totalFee+=90;

if (this.getField("Exam2").value!="Off") totalFee+=90;

totalFee+=Number(this.getField("TutorialFee").value)*90;

event.value = totalFee;

sw777
sw777Author
Known Participant
March 20, 2017

Wow! That's pretty simple. Not sure where I was going with the hidden textboxes. :-)

Just a minor glitch in that the "TotalFee" produces an incorrect value, but if I change "TutorialFee" to "TutorialSessions" it works perfect.

TutorialFee is a separate custom calculation that I managed to work out myself.

Thanks for your help.

P.S. Any suggestions on where I can access some JavaScript tutorials starting from the basics? I'd really like to know a bit more about the structure of the code rather than just cobbling together other people's suggestions through a trial and error approach..

try67
Community Expert
Community Expert
March 20, 2017

That is not a minor thing. It usually means that the field calculation order is incorrect. You need to fix it if you want it to work correctly.

Regarding learning JavaScript: Most tutorials online are about JS in web-pages. The variant of JS that runs in Acrobat shares the same basic syntax with web-based JS, but has different objects, methods and properties.

This is a pretty good general JS tutorial:

http://www.w3schools.com/js/default.asp

For PDF-specific JS check out:

http://www.adobe.com/devnet/acrobat.html

https://acrobatusers.com/tutorials/

http://www.pdfscripting.com/