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

Survey skip logic in PDF form

Explorer ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

Hi,

I am creating a basic PDF form survey with radio button yes/no questions and next / back buttons to show each page. When the user pushes the "next" button on page 20, page 21 needs to be shown only  IF  3 previous questions on depression (1,2, and 3) OR three previous questions on irritability (6,7, and 8) were answered yes (yes = 1, no = 0).

Here is the code that is ALMOST working:

Document level JS

var depressionchecklist = this.getField("Q1"+"Q2"+"Q3").valueAsString;

var irritabilitychecklist = this.getField("Q6"+"Q7"+"Q8").valueAsString;

JS for the "next" button on page 20

if ( depressionchecklist == "3" || irritabilitychecklist == "3"  ) {

this.pageNum = this.pageNum = 20;}

else {this.pageNum = 21; }

Right now, even when the depression and irritability questions are all answered yes, when I hit the next button on page 20, it automatically skips to page 22 (it reverts to the else statement).

It's probably some simple mistake with the code.... Any ideas?

JS.JPG

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.2K

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
LEGEND ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

This

var depressionchecklist = this.getField("Q1"+"Q2"+"Q3").valueAsString;

gets the value of a single field called "Q1Q2Q3".

I'm guessing that isn't what you want: you're probably trying to take a shortcut but you do need to get each field separately.

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
Explorer ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

Ok, interesting. I tried this:

var depressionchecklist = this.getField("Q1").valueAsString + this.getField("Q2").valueAsString + this.getField("Q3").valueAsString;

var irritabilitychecklist = this.getField("Q6").valueAsString + this.getField("Q7").valueAsString + this.getField("Q8").valueAsString;

But still not working...

Does this look right?

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
Explorer ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

Maybe I need to use integers instead of strings? Since I want to add them and see if the sum = 3?

Any thoughts on that?

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 ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

Use the "value" property instead of "valueAsString".

The issue can also be forced by casting the field value as a Number, like this.

var depressionchecklist = Number(this.getField("Q1").value) + Number(this.getField("Q2").value) + Number(this.getField("Q3").value);

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
Explorer ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

So I'm throwing out the OR statement for now to simplify things, and this is what I'm trying:

Document Level JS

var depressionchecklist = Number(this.getField("Q1").value) + Number(this.getField("Q2").value) + Number(this.getField("Q3").value);

Button JS (on page 20)

if ( depressionchecklist == 3 ) {

this.pageNum = this.pageNum = 20;}

else {this.pageNum = 21; }

This doesn't seem to work, as the button defaults to the else statement even if Q1, Q2, and Q3 are all answered yes.

Remember, the value of each question is a 0 (for no) and a 1 (for yes), however I assigned these myself and I'm sure they are strings, because I could have called them "Choice one" and "Choice two".

I seem to be going in circles here...

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 ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

Why are you using a doc-level script to define the depressionchecklist variable? That means it's only updated once.

Move it to the other script.

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
LEGEND ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

Add an app.alert to report the value of depressionchecklist. This is the basic route to solving problems in code - see that the values are what you expect.

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
LEGEND ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

Hmm, and why is this in document level JavaScript, which is run only once, when the file is first opened, and before the user fills in the form...?

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
Explorer ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

Great question! I realized that it shouldn't be in the document level JavaScript at all... New to this

With all of your help, I got the whole thing working! This is what I did:

var depressionchecklist = this.getField("Q1").valueAsString + this.getField("Q2").valueAsString + this.getField("Q3").valueAsString;

var irritabilitychecklist = this.getField("Q6").valueAsString + this.getField("Q7").valueAsString + this.getField("Q8").valueAsString;

if ( depressionchecklist == "111" || irritabilitychecklist == "111" )

{this.pageNum = 20;}

else {this.pageNum = 21; }

I realized that adding strings was still an option, it just needed to check for "111" instead of 3.

Might be a sketchy solution, but quick nonetheless.

I didn't do the app.alert... not sure how that works - would it have made the code cleaner?

Thanks again!

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 ,
Jan 10, 2019 Jan 10, 2019

Copy link to clipboard

Copied

LATEST

If you want to add values as numbers, then you need to make sure they are numbers and not strings.

To do that change this line:

var depressionchecklist = this.getField("Q1").valueAsString + this.getField("Q2").valueAsString + this.getField("Q3").valueAsString;

To:

var depressionchecklist = Number(this.getField("Q1").valueAsString) + Number(this.getField("Q2").valueAsString) + Number(this.getField("Q3").valueAsString);

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
LEGEND ,
Jan 10, 2019 Jan 10, 2019

Copy link to clipboard

Copied

app.alert isn't something to leave in your code (normally), but it is hugely useful in problem solving (debugging).

Every programmer, at every level, ends up asking themselves "why isn't this working? Everything looks right". This is where it is really useful to put out a message showing the value of important variables. If you'd added

app.alert("Value of depressionchecklist just before the if statement is " + depressionchecklist)

it would have showed you that the value wasn't what you expected. My code is often full of things like this as I try to chase down the causes of weird stuff.

Caution! It's easy to get stuck with the same message over and over, especially in things like calculation scripts that can be called over and over. No problem, just kill Acrobat with Task Manager or Activity Monitor. YOU WILL LOSE ANY CHANGES YOU DIDN'T SAVE !

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 ,
Jan 10, 2019 Jan 10, 2019

Copy link to clipboard

Copied

Or press Esc repeatedly and hope that one of those clicks will cause the script to stop executing...

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