Skip to main content
Participating Frequently
May 10, 2019
Answered

If/Else Statement from text to numbers in PDF Form

  • May 10, 2019
  • 2 replies
  • 4378 views

Hello, I have been reading few answers in this forum but none is about my case. I'm trying to add an IF / ELSE statement to a quote form in PDF but is not working and obviously I'm writing a wrong code.

I have a drop list called "STATE" and another text field called "FL_CALC". Now my question is how can I get a numerical result result in "FL_CALC" field depending the selection made in "STATE"  field?

All that I need is: that number "145" appear in "FL_CALC" field when you choose FL state in "STATE" field... if you choose any other state, then in "FL_CALC" appear 450. Is this possible?

This topic has been closed for replies.
Correct answer try67

Sure. Use this code as the custom calculation script of "FL_CALC":

event.value = (this.getField("STATE").valueAsString=="FL") ? "145" :  "450";

2 replies

Karl Heinz  Kremer
Community Expert
May 10, 2019

I would recommend that you learn how JavaScript works. It's not overly complex, and you can probably learn a lot about it in just an afternoon. A while ago I wrote a blog post about how to learn to program in JavaScript for Acrobat: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

In general, an if else statement looks like this:

if (somethingIsTrue) {

  // .... if block

}

else {

  // .... else block

}

If "somethingIsTrue" is true, then the "if block" gets executed, otherwise the "else block".

So, in your example, the user picks the "STATE", and when STATE is set to "FL", you want FL_CALC to be 145, and otherwise you want FL_CALC to be set to 450:

if (this.getField("STATE").value == "FL") {

  this.getField("FL_CALC").value = 145;

}

else {

  this.getField("FL_CALC").value = 450;

}

This does not contain any error checking (e.g. what happens if the field "STATE" or "FL_CALC" does not exist?), so you may want to look into some exception handling.

The code as I've described it will work from e.g. a global calculation script. If you want to do a calculation script, you will need to set the return value for the event handler: You want to calculate FL_CALC based on the "STATE" field, so you will need to create a custom caulcation script for the field FL_CALC. There are more elegant ways to do this, but to keep it easy to understand, I would recommend you use something like  this:

if (this.getField("STATE").value == "FL") {

   event.value = 145;

}

else {

  event.value = 450;

}

Participating Frequently
May 10, 2019

Thanks Karl... I will give a try tomorrow when I be back to my office.... thanks for taking time to answer me....

try67
try67Correct answer
Community Expert
May 10, 2019

Sure. Use this code as the custom calculation script of "FL_CALC":

event.value = (this.getField("STATE").valueAsString=="FL") ? "145" :  "450";

Participating Frequently
May 11, 2019

Ok, first at all... THANK YOU !!!...works perfect, but....

as you can see in the above pict. the total field created is always shown the value $ 450, (because we set the custom script for "FL_CALC" field in my first question). As we set FL = 145 and every else = 450, this rule include the "--- Select State ---" option as well, but there is a chance to exclude this first option and to set FL = 145, else = 450, and --- Select State --- = 0 ?? everything in the same custom calculation script ?... I prefer that every time that open this PDF or push the CLEAR button, value shown be zero (0), since is the total result for a quote, and doesn't looks good even before to start filling up the form that $ 450 appears and you don't know from where... I been trying but I mess up everything with not result at all.

Thanks again in advance.

Participating Frequently
May 13, 2019

1. That usually happens because the file is opened using the browser's internal PDF plugin, and most of those plugins are sub-part, at best. Instruct your users to not open the file in the browser, but to save it locally and then open it using Adobe Reader. It's a bit of a hassle, but it will work much better.

2. To do that you would need to define some condition as to when to update the field, and when not.

For example, you can include a (hidden) check-box in the file and use the script that submits it to tick it before sending, and then adjust the code that populates the field with the time-stamp to only do so if that field is not ticked.


Thanks try67 for you answer, about # 1 I can force users to download it instead to open it... I was leaving this option for the end, but is OK.

Now, about # 2, I got your idea: a hidden check box that is check it when I click "submit by email" and then change the condition of "QUOTE No." field.... but like I say before: my humble knowledge is not so extensive... and here is what happen now:

I take out the "save" button to just have PRINT - SUBMIT BY EMAIL - and CLEAR FORM, and added a hidden check box next to "SUBMIT..." field and when I tried to play with java scripts of SUBMIT..." and "Quote No." buttons do nothing... in fact, now I notice that calculations doesn't refresh if you change any value and you can hit calculate as many times as you wish... then only the result of quote ($ value) change and refresh if you check or uncheck any of the check box placed at the start of the form... I guess I mess up one more time. If I told what codes I used on those fields, can you help me out ?