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

Using Combo Boxs to Prepopulate Form Field

MJF
Registered: Jan 18 2007
Posts: 33

I am VERY new to Acrobat and need some help with the following.
I have created a form with the help of your article titled " Changing Another field with Combo Box Selection"
My form consists of 10 dropdown combo Boxes all containing a list of product codes. When I chose a product code from the list, I need the corresponding product name to automatically prepupolate in a text field underneath it. I was able to accomplish this for the first dropdown box by using the sript sample provided in the article but I am unable to make it work for the other dropdown boxes.
Here is an example of my form: I have a Combo Box labeled "Product Code.0.0" that has a list of product codes. I then have several other Combo Boxes labeled "Product Code.0.1, Product Code.0.2 and so on(all boxes have the same dropdown list items).
Then I have a Text Box underneath each Combo Box labeled "Product Name.0.0, ProductName.0.1 etc. that I need prepopulated when I chose an item in the combo box above.
How do I make the first Combo Box and Text Box work indenpendently of the next set of Combo/Text Boxes?

My Product Information:
Acrobat Pro 8, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
In the article I use hard coded field names for populating the result of the selection. Since you have multiple ComboBoxes with identical contents you need some way to automatically generate the field name. If you had several fields to populate at once from each combo I'd say that you need to use a parallel naming convention between the comboboxes and the text fields. In fact you already have this, i.e., "ProductCode.0.1" matches "ProductName.0.1"(Good thinking up front) . You can pass the name of the combobox into the function that does the prepopulation and then use JavaScript string manipulation functions to generate the name of the text field. Take a look at this article

[url=http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/string_splitting/]http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/string_splitting/[/url]

However since you're only dealing with one text field there is a much easier way. Just pass the text field name into the function that does the prepopulation. So your custom keystroke event will look like this.
if( event.willCommit ){if(event.value == "")this.resetForm(["DeptContact","DeptEmail","DeptNumber"]);elseSetFieldValues(event.value, "ProductName.0.1");}
Then the document level script that does the field population would look like this.

function SetFieldValues(cDeptName, cFldName){this.getField(cFldName).value = DeptData[cDeptName].cProdName;}
Of course I'm using the code from the article, you'll have to adjust it to your setup.

A couple of other things. It really is better to use the string manipulation to automatically create the field names. For the easy method I've outlined above the code in each combobox is different by one thing, the name of the field to be populated. In the automated method the code is exactly the same everywhere. The only difference the in the name of the fields themselves. This make for more robust and easier to maintain code. And another thing, the code in the article was written to be easy to follow. A much better design would be to put a single function call into the Keystroke event of the combobox. This way all the code is in a single location. You can change how all the comboboxes work by only changing code in a single location. The trick to doing this is called abstraction. You have to be able to generalize the code to work with any comboboxes by using it's name. This is kind of an advanced thing.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

MJF
Registered: Jan 18 2007
Posts: 33
Hi Thom
I tried my interpretation of your scripting and I was unable to make it work. I am sure my sripting is not correct. Here is what I put for the custom keystroke event in ComboBox labelled ProdCode.0.0

if(event.willCommit)

{
if(event.value == "")
this.resetForm(["ProdName"]);

else
SetFieldValues(event.value,"ProdName.0.0");
}

At the document level script I put:

//Place all prepopulated data into a single data structure
var ProdData = {P#123:{name:"Paper"},E#123:{name:"Erasers"}, S#123:{name:"Staples"}};
function SetFieldValues(cProdCode, cProdName)
{
this.getField("cProdName").value = ProdData[cProCode].cProdname;
}

Like I said excuse my ignorance, since this is all new to me.
P#123, E#123 and S#123 represent the items in the list of product codes in the DropDown Boxes. Paper, Erasers and Staples are the corresponding Product Names. When I chose a product code from the 1st dropdown box I only want the product name to populate once in the text field underneath it. Then I go on to the 2nd dropdownbox and chose an item to be populated in the 2nd text field underneath and so on.

Please let me know how I can fix the sripting to make this work. Also, I used the scripting from your article "Changing Another Field with Combo Box Selection" because I am totally new to JavaScript. Therefore if there is an easier way to script this question please explain it to me.
ThankYou
MJF
Registered: Jan 18 2007
Posts: 33
I have finally made my form work!
Just in case anyone else is interested, here is the scripting I used:
In the Combo Box Properties Format Custom Custom Keystroke Script Edit
I entered:

if( event.willCommit )
{
if(event.value == "")
this.resetForm(["ProdName.0"]);
else
SetFieldValues(event.value,”ProdName.0”);
}(Change the ProdName field in each Combo Box to correspond to the text field name below it. E.g. “ProdName.0” , ”ProdName.1” etc.)

In the Advance Document Processing Document JavaScript JavaScript Functions
I eneterd:
Script Name: SetFieldValues Add

//Place all prepopulation data into a single data structure
var ProdData = {a123:{name:"Apple"},
b123:{name:"Banana"},
p123:{name:"Pear"},
o123:{name:”Orange”}};
function SetFieldValues(cProdCode,cProdName)
{
this.getField(cProdName).value=ProdData[cProdCode].name;
}