6 Replies Latest reply on Mar 20, 2017 3:30 PM by sw777

# Calculating a total based on checkboxes and numeric fields

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.

• ###### 1. Re: Calculating a total based on checkboxes and numeric fields

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;
```
• ###### 2. Re: Calculating a total based on checkboxes and numeric fields

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..

• ###### 3. Re: Calculating a total based on checkboxes and numeric fields

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/

• ###### 4. Re: Calculating a total based on checkboxes and numeric fields

Fixed as follows, which seems to work perfect...

// Code for "TutorialFee" field

event.value = (this.getField("TutorialSessions").value * 90);

/// Code for "TotalFee" field

var totalFee = 0;

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

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

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

event.value = totalFee;

• ###### 5. Re: Calculating a total based on checkboxes and numeric fields

Maybe I misunderstood the issue... If it works, great!

• ###### 6. Re: Calculating a total based on checkboxes and numeric fields

Thanks for you help yet again, as this isn't the first time you've pointed me in the right direction.

Hopefully I can start to work some of this stuff out myself with the links you've provided.

Cheers.