These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Form script help: checkboxes and text fields

Yosh
Registered: Aug 11 2011
Posts: 7
Answered

I have a form in which on page 1 users check different items, and I need the items that they checked to appear in a read-only list on page 3. For example, if there are 10 possible answers and the user checks only four of them, I want just those four to be listed on page 3.
 
I think that what I need to do is create a read only field on page 3 and place a script in that field, but I'm not sure what script would work best.
 
I'd greatly appreciate any suggestions from the forum.
 
Thanks!

My Product Information:
Acrobat Pro 10.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
You could set up the text field with a custom calculate script. The script would loop through the check boxes on page 1, and for each one whose value is not "Off", add it's description to a string, separated by one or two carriage returns perhaps. After the loop, set event.value to the string that's been built up. If you need help with the specific code, post what you've come up with so far and someone can suggest changes.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Yosh wrote:
I think that what I need to do is create a read only field on page 3 and place a script in that field, but I'm not sure what script would work best.
That is one way but this could affect form performance, but it is the easiest way.

event.value = this.getField("CheckBox 1").value + ", " + this.getField("CheckBox 2").value + ", " + this.getField("CheckBox 3").value + ", " + ... + this.getField("CheckBox 10").value;

But it does not adjust for unchecked items, does not eliminate values of "Off" (unchecked fields), and includes the ", " separators when there is not data.

The following scirpt builds an array of the values of check box's values not equal to 'Off".

// array of field names to process
var aCB = new Array("CheckBox.0", "CheckBox.1", "CheckBox.2", "CheckBox.3", "CheckBox.4", "CheckBox.5", "CheckBox.6","CheckBox.7", "CheckBox.8", "CheckBox.9")
var sCB = new Array();
for(i = 0; i < aCB.length; i++) {
if(this.getField(aCB[i]).value != "Off") {
sCB[sCB.length] = this.getField(aCB[i]).value
} // not equal to "Off"
} // end for aCB
event.value = sCB.join(", ");

George Kaiser

Yosh
Registered: Aug 11 2011
Posts: 7
I tried this but it didn't work-- the text field remains blank when these checkboxes are checked:

{
event.value = this.getField("Checkbox 1.0").value + ", " + this.getField("Checkbox 1.1").value + ", " + this.getField("Checkbox 1.2").value ;

}

To clarify further as to what I want the form to do, here is an example: If each checkbox is for users to select their favorite fruits and a user checks off Grapes, Oranges, and Apples; I want just those three values to appear in a separate read-only text field on a subsequent page.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
Are you getting any errors in the JavaScript console?

Have you set the export value of the check box?

Spelling and capitalization of field names, properties, methods, functions, etc is very important.

Assuming the default name for the Check Box field, creating one field, and placing multiple. Export value of each Check Box has been changed as needed.

// custom calculation script for text field;
// get fields as an array;
var aFields = this.getField("Check Box1").getArray();
// array of non-Off values;
var aSelected = new Array();
// loop through fields;
for(i = 0; i < aFields.length; i++) {
if(aFields[i].value != "Off") {
// add value to new element in selected array
aSelected[aSelected.length] = aFields[i].value;
} // end if not Off;
} // end loop of fields
// set field's value;
event.value = aSelected.join(", ");
// end of custom calculation script;

George Kaiser

Yosh
Registered: Aug 11 2011
Posts: 7
George, that worked just fine. Thank you.

One more question--do you know how I can get line breaks in between each value rather than the commas?
Yosh
Registered: Aug 11 2011
Posts: 7
I figured out the answer to my own question: use "\n" in place of the ", "

Thanks again for your help--the script works great!
Yosh
Registered: Aug 11 2011
Posts: 7
Well, I do have one more question-- is there an if statement that I can add to the script so that it doesn't include commas (or line breaks) for fields that the user leaves blank? Or would I need to use a different script altogether?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The created array only has check boxes for which there is a check box field value of anything other then "Off". So unless the export value for the check box is a line break or a coma, there is nothing else to do.

Are you talking about using fields other than check boxes or radios buttons?

George Kaiser

Yosh
Registered: Aug 11 2011
Posts: 7
Yes, I that's right-- the check box values are exporting fine, but I am also using this script elsewhere to calculate an array of text box fields. To give you more detail on what is happening, the field where I want the array to appear shows a bunch of commas until the user fills in the text fields that are being exported. What I want to happen is that nothing at all shows up in the destination field if the user hasn't yet entered any values in the text fields.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Since the provided script relies on the un-selected value of a check box or radio button not being selected being equal to "Off", it might not work for a text field if any of the possible entries should be "Off".

Change the test or "Off" to test for the "", or null string, for the text fields.

George Kaiser

Yosh
Registered: Aug 11 2011
Posts: 7
That worked like a charm, George-- such an easy fix too. Thanks again!!