• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Feb 27, 2018 Feb 27, 2018

Copy link to clipboard

Copied

Dear All,

adobe-question.jpg

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;

})();

Thank you in advance.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

621

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2018 Feb 27, 2018

Copy link to clipboard

Copied

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

https://acrobatusers.com/tutorials/conditional-execution

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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

Copy link to clipboard

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Replace:

if( v1 ="Female")

With:

if( v1=="Female")

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thanks try67,

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Thank you try67 - I have sent you a link.

Votes

Translate

Translate

Report

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