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

Populating text field from multiple checkbox inputs

DTjava
Registered: Jun 17 2011
Posts: 21
Answered

Hi everyone - First time poster, javascript skills minimal.
 
I'm building up a product catalog, and I want to add a checkbox to every product. If you check the box, the name of the product gets added to a read only text field on the last page (our order form).
 
I used something like this and it worked for one item:
 
var one = this.getField("Check Box1");
var two = this.getField("Text2");
if (one.value == 'Yes') {
two.value='product'
} else if (one.value == 'Off') {
two.value=''
}
 
The PROBLEM is when I want more than one product checkbox to work. If I use the same script on a second or third checkbox, it overwrites the existing entry in the text field. What I want is for an item to show up in the text field for every box checked off, in a neat little list. If you go back and check off a box, it disappears from the text field, leaving the remaining items.
 
I feel that I'm close to a solution here, but I can't get the text field to hold onto the info. Any help would be appreciated!

My Product Information:
Acrobat Pro 9.0, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
I do not think this is that simple.

If you just concatenate the values of the check boxes with a ',' separator one would end up with something like for 4 check boxes:

"OffOffOffOffOffOffOff"
or
"Item 1OffItem 3OffOffOffOff"

If one changes "Off" to Null then one might have:

",,,,,,,"
or
"Item 1,,Item 3,,,,"

But I think you would want something like:
""
or
"Item 1, Item 2"

Which means one needs a some scripting to handle null values and not only skip the value but also not include the separator.

The following document level script can concatenate three values and optionally include a separator as needed and adjust for null values

function fillin(s1, s2, s3, sep) {
/*
purpose: concatenate up to 3 strings with an optional separator
inputs required:
s1, s2, s3 - character string to be joined together
optional:
sep - separator string to be inserted between strings to be joined
returns:
combined strings
*/
var test = 0; // value to determine how to join strings
var sNew = ''; // returned concatenated string

// account for optional sep parameter not being present
if(typeof sep == "undefined") { sep = "";}

s1 = s1.toString();
s2 = s2.toString();
s3 = s3.toString();

// figure which parameters to process based on logical bit values
if (s1 != "") { // set 1 bit
test |= 1;
}
if (s2 != "") { // set 2 bit
test |= 2;
}
if (s3 != "") { // set 4 bit
test |= 4;
}

// process parameters based on above action
switch (test.toString()) {
case '0': // no passed parameters - 000
sNew = '';
break;
case '1': // only s1 - 001
sNew = s1;
break;
case '2': // only s2 - 010
sNew = s2;
break;
case '3': // s1 and s2 - 011
sNew = s1 + sep + s2;
break;
case '4': // only s3 - 100
sNew = s3;
break;
case '5': // s1 and s3 - 101
sNew = s1 + sep + s3;
break;
case '6': // s2 and s3 - 110
sNew = s2 + sep + s3;
break;
case '7': // all three - 111
sNew = s1 + sep + s2 + sep + s3;
break;
default:
// unknown condition
app.alert('Unknown situation for \'fillin\' function', 0, 0);
sNew = '';
break
} // end switch
return sNew;; // return concatenated string
} // end fillin function


You can then use the following custom calculation script in the custom calculation script for the text field:

(function() {
// array of check boxes names
var aNames = new Array("Check Box.0",
"Check Box.1", "Check Box.2", "Check Box.3",
"Check Box.4", "Check Box.5", "Check Box.6");
// array for values from fields
var aItems = new Array(aNames.length);
// fill array with selected values
for(i = 0; i < aNames.length; i++) {
// get field value
aItems[i] = this.getField(aNames[i]).value;
// change "Off" value to null value
if(aItems[i] == "Off") { aItems[i] = ""; }
} // end loop through fields
// clear field of listed items
event.value = "";
// build string of selected items
for(i = 0; i < aItems.length; i++) {
event.value = fillin(event.value, aItems[i], "", ", ");
} // end loop through items;
}) ();

You may need to adjust the array of check box field names to match you form.

George Kaiser

DTjava
Registered: Jun 17 2011
Posts: 21
George - That was amazing. It worked PERFECTLY! You just saved me about 2 weeks of piecing together code! Mad props.
DTjava
Registered: Jun 17 2011
Posts: 21
Hi George - I have a follow up question: I need to extend this code past 7 products. When I add additional check boxes, the new box doesn't output to the text field. Instead data from an earlier check box gets inserted instead. Any way to continue this chain of code to allow for many more products?

Thank you so much for all of your help!



DTjava
Registered: Jun 17 2011
Posts: 21
Hmm - I may have just figured it out. I think there was either a conflic with a doc level code, or I screwed up when I created the check boxes...I copied and pasted rather than created a new one each time. Building up the product check boxes one at a time and adding an extra checkbox entry into the array field seemed to do the trick.

Is there a way to add a carriage return after each item entry, creating a more scrollable and manageable list?