## How to do an if then statement based on the value of a field on a PDF form

Community Beginner ,
Feb 27, 2018

Copied

Dear All,

I hope somebody can help me. I'm not a programmer but need to create some calculations within a PDF form. I am trying to calculate the number of daily calories required based on a number of criteria. I have managed to make the calculation work if I am just looking at the values of each field but I need to take into account if the user is male or female. For example: If the user is male then use: 1066 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2) and If the user is Female use: 66 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2).

I am performing the calculation in the Calories field.

This is what I have so far which does work but doesn't have the capacity to change between male or female.

(function() {

var v1 = +getField("Gender").value;

var v2 = +getField("Age").value;

var v3 = +getField("Height").value;

var v4 = +getField("Weight").value;

var v5 = +getField("Activity").value;

var result = 66 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2);

event.value = result;

})();

TOPICS
Acrobat SDK and JavaScript, Windows

Views

228

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

## How to do an if then statement based on the value of a field on a PDF form

Community Beginner ,
Feb 27, 2018

Copied

Dear All,

I hope somebody can help me. I'm not a programmer but need to create some calculations within a PDF form. I am trying to calculate the number of daily calories required based on a number of criteria. I have managed to make the calculation work if I am just looking at the values of each field but I need to take into account if the user is male or female. For example: If the user is male then use: 1066 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2) and If the user is Female use: 66 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2).

I am performing the calculation in the Calories field.

This is what I have so far which does work but doesn't have the capacity to change between male or female.

(function() {

var v1 = +getField("Gender").value;

var v2 = +getField("Age").value;

var v3 = +getField("Height").value;

var v4 = +getField("Weight").value;

var v5 = +getField("Activity").value;

var result = 66 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2);

event.value = result;

})();

TOPICS
Acrobat SDK and JavaScript, Windows

Views

229

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 27, 2018 0
10 Replies 10
Feb 27, 2018

Copied

Here's an article on how to write an if statement in a calculation:

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 27, 2018 0
Community Beginner ,
Feb 28, 2018

Copied

Thanks Thom,

I nearly have it working with:

var v1 = this.getField("Gender").valueAsString;

var v2 = this.getField("Age").value;

var v3 = this.getField("Height").value;

var v4 = this.getField("Weight").value;

var v5 = this.getField("Activity").value;

if( v1 ="Female") event.value = 1066 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2);

else event.value = 66 + (6.23 * v4) + (12.7 * v3) - (6.8 * v2);

The problem is that it only recognises the 'Female' equation. If I change the Gender to Male it doesn't change the equation. Could this be something to do with the lack some sort of 'After Update', 'Requery' or 'Refresh function assigned to the other boxes?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2018 0
Most Valuable Participant ,
Feb 28, 2018

Copied

Replace:

if( v1 ="Female")

With:

if( v1=="Female")

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2018 0
Community Beginner ,
Feb 28, 2018

Copied

Thanks try67,

Unfortunately the Calories field is still not updating after update of Gender Field.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2018 0
Most Valuable Participant ,
Feb 28, 2018

Copied

Press Ctrl+J and check the JS Console for errors. Also, you have to change the value of one of the fields for it to "kick in".

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2018 0
Most Valuable Participant ,
Feb 28, 2018

Copied

One more thing: Since the Gender field is a drop-down you should enable its option to commit the selected value immediately (under Properties - Options). Otherwise it will only update once you leave the field, not the moment a selection is made.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2018 0
Most Valuable Participant ,
Feb 28, 2018

Copied

Same goes for Activity, although it doesn't seem like you're actually using its value in the formula (v5 is declared but not used).

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2018 0
Community Beginner ,
Feb 28, 2018

Copied

Dear try67,

Thank you so much for your help.

The suggestions didn't work with the drop down list but I changed it to a text field and the if statement now works.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2018 0
Most Valuable Participant ,
Feb 28, 2018

Copied

That's strange... If you want to share the file with me I'll be happy to check out what the issue is.

You can upload it to Dropbox, Google Drive, Adobe Cloud, etc., and post the link to it here, or you can email it to me to try6767 at gmail.com.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 28, 2018 0
LATEST
Community Beginner ,
Feb 28, 2018