Change global variables based on radio button selection
Copy link to clipboard
Copied
I have a real simple problem: I have a form with a radio button group called "Membership Status". The two options are "Member" and "Non-Member". When "Member" is selected, I want to change the values of a set of global variables one way, and when "Non-Member" is selected, I want to change the values another way. Where do I put this code (e.g., as a Javascript action on each separate radio button option, on a document Javascript, etc), and what's the syntax to get me started?
Copy link to clipboard
Copied
I would use the Calculation script of a (hidden) text field to do it. Like this:
var memStatus = this.getField("Membership Status").valueAsString;
if (memStatus=="Member") {
global.var1 = "ABC";
global.var2 = 1234;
} else if (memStatus=="Non-Member") {
global.var1 = "DEF";
global.var2 = 5678;
} else {
global.var1 = "";
global.var2 = 0;
}
The reason for doing it like that is that it would also update if the form is cleared, for example, which a Mouse Up script on the radio-buttons themselves won't do.
Copy link to clipboard
Copied
When would this script calculate? As soon as the PDF is opened? Would it recalculate each time the radio button choice changes?
Copy link to clipboard
Copied
Each time the value of any field in the file is changed. If you want it to also update when the file is opened add this command under a doc-level script (NOT in a function):
this.calculateNow();
Copy link to clipboard
Copied
Thank you very much!
Copy link to clipboard
Copied
You should also know that global variables are protected in the scripting model. The form will only be able to set and get the variable that is has already set. If you save the form in a way that changes the DocID, these variable will not be accessible in that copy.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Are you sure about that? Doesn't that defeat the whole point of a global variable?
Copy link to clipboard
Copied
It's one of the preference settings in the JavaScript Category, since Acrobat 9 I think.
I'm not sure if changing the minor part of the docID will block global varible access. But I know changing the major part will.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Ah, you mean the "Enable global object security policy" box? I always instruct my clients to turn it off whenever I have a script that uses global variables. Without doing that it's pretty much useless, indeed.

