Copy link to clipboard
Copied
I have created a form, which is nearly complete. The only thing I have left, that I'm not sure how to do is this:
If the selection in the [Program] dropdown box is "Biology",
then the field [Address] should be "303 McKinley Hall",
field [Phone] should be "(800) 238-4732", and
field [Email] should be "Biology@itsu.edu".
I can't seem to get the code right to automatically populate the Address, Phone, and Email fields based on the option chosen in [Program]. I'm probably over looking something very simple.
Copy link to clipboard
Copied
As 'Validate' script of dropdown field use this:
var address = this.getField("Address");
var phone = this.getField("Phone");
var email = this.getField("Email");
if(event.value == "Biology"){
address.value = "303 McKinley Hall";
phone.value = "(800) 238-4732";
email.value = "Biology@itsu.edu";}
//If you have more conditions to add addresses, phones, email add 'else if' here
else{
address.value = "";
phone.value = "";
email.value = "";}
Copy link to clipboard
Copied
The template field re-naming pattern is fixed, it cannot be changed. So your scripts have to be written to take this naming into account when accessing the fields.
Copy link to clipboard
Copied
As 'Validate' script of dropdown field use this:
var address = this.getField("Address");
var phone = this.getField("Phone");
var email = this.getField("Email");
if(event.value == "Biology"){
address.value = "303 McKinley Hall";
phone.value = "(800) 238-4732";
email.value = "Biology@itsu.edu";}
//If you have more conditions to add addresses, phones, email add 'else if' here
else{
address.value = "";
phone.value = "";
email.value = "";}
Copy link to clipboard
Copied
Nesa thank you for this. I was able to make it work on my end. It works great on static form. I have a question, my PDF form has a template which I can spawn new pages if needed. The template itself works. But when I click on the button to add a page on my document the spawn page comes with a prefix of P3.page1.phone (P3 changes in reference to the page number. page1 remains the same. phone remains the same). I have explored using the var prefix but it did not work. Any idea how I can make it this work?
I tried using a document level java too but did not work and I am unsure why not. I think I would like to stick with my field Java if possible.
Copy link to clipboard
Copied
In the script to spawn template, set field rename to false (bRename property).
Copy link to clipboard
Copied
Thank you, Nesa! Yes, that is an option, but when the renaming is set to false, the fields will have the same therefore whatever I enter on one page, it will duplicate on the other.
Copy link to clipboard
Copied
The template field re-naming pattern is fixed, it cannot be changed. So your scripts have to be written to take this naming into account when accessing the fields.
Copy link to clipboard
Copied
This question is asked a lot!!
Here are some articles that discuss list programming.
https://acrobatusers.com/tutorials/change_another_field/
https://acrobatusers.com/tutorials/list_and_combo_in_lc/
https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm
Copy link to clipboard
Copied
Ok, thanks for the help, but I guess I need to explain further. I have a list of 20 programs, and 5 fields to work with. I want Fields 2-5 populated based on the drop down menu choice from Field 1.
Field 1: Program
Field 2: Address
Field 3: CSZ (City/State/Sip)
Field 4: Dept Code
Field 5: Participants (number of participants)
Would it be easier if I program each of the four fields individually based on Field 1 rather than trying to populate all of them from Field 1? To be honest, I haven't done this in a long time, and back then I did it with "if, else" statements, so I'm trying to wrap my head around how javascript compares to the "old way".
Copy link to clipboard
Copied
You can easily add more fields to the script I posted above (it's also using if/else).
Copy link to clipboard
Copied
Thank you again. I'm still missing something, and it is probably something very simple . . . it almost always is. So here is a sample of my code and the results. No matter which program I choose, it will not populate the other fields. What am I missing?
var address = this.getField("address");
var phone = this.getField("phone");
var email = this.getField("email");
if(event.value == "Anthropology"){
address.value = "541 Sampson Hall ";
phone.value = "(555) 223-4419";
email.value = "anthro@ist.edu";}
if(event.value == "Biology"){
address.value = "219 Brownston Hall";
phone.value = "(555) 223-5243";
email.value = "biol@ist.edu ";}
if(event.value == "Communicatons"){
address.value = "330 Marquette Hall";
phone.value = "(555) 223-6639";
email.value = "comm@ist.edu ";}
if(event.value == "English"){
address.value = "411 Emerson Hall";
phone.value = "(555) 223-7288";
email.value = "engl@ist.edu ";}
if(event.value == "Psychology"){
address.value = "339 Haverty Hall";
phone.value = "(555) 223-4389";
email.value = "psyc@ist.edu ";}
Copy link to clipboard
Copied
Script looks ok, where do you use it?
Check that field names are correct.
Can you share a file with us?
Copy link to clipboard
Copied
How do I share the file?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
1. Use the script in 'Validate' tab.
2. Your field name are not correct, "email" and "Email" is not same, JavaScript is case sensitive.
3. In dropdown field under 'Options' tab check 'Commit selected value immediately', if you want to show values as soon as you select program (if not checked you need to click outside the field once).
4. Added else if to the script instead of just 'if' and last else to set fields to blank.
Here is your file with changes made (script is in 'Validate' tab of dropdown field):
https://drive.google.com/file/d/1_4vdMaDyEBPPf0PJkH9t9urfsgwXyPGD/view?usp=sharing
Find more inspiration, events, and resources on the new Adobe Community
Explore Now