This topic is locked
[SOLVED]

 Autofill Fields Based On Checkbox

9/15/2017 1:38:33 PM
ASPRunner.NET General questions
I
i.NoLim author

I'm building a form that has a checkbox, "SAME_AS_ABOVE," and the following fields:
CONTACT_LAST_NAME

CONTACT_FIRST_NAME

EMERGENCY_FIRST_NAME

EMERGENCY_LAST_NAME
What I'm attempting to do is if the user selects the "SAME_AS_ABOVE" checkbox the emergency fields should auto populate with the contact information. This is what I got so far but its obviously not working, any help will be appreciated.



var ctrlSAME_AS_ABOVE = Runner.getControl(pageid, 'SAME_AS_ABOVE');

var ctrlLAST_NAME = Runner.getControl(pageid, 'LAST_NAME');

var ctrlFIRST_NAME = Runner.getControl(pageid, 'FIRST_NAME');

var ctrlCONTACT_LAST_NAME = Runner.getControl(pageid, 'CONTACT_LAST_NAME');

var ctrlCONTACT_FIRST_NAME = Runner.getControl(pageid, 'CONTACT_FIRST_NAME');
ctrlSAME_AS_ABOVE('change', function(e)

{

if(this.getValue() == 'on')

{

 ctrlLAST_NAME.setValue (ctrlCONTACT_LAST_NAME.getValue());

ctrlFIRST_NAME.setValue (ctrlCONTACT_FIRST_NAME.getValue());

}

})
T
Tim 9/15/2017

I'm not sure, but as a quick test, have you tried this.getValue() == '1' instead of == 'on'?

I
i.NoLim author 9/15/2017



I'm not sure, but as a quick test, have you tried this.getValue() == '1' instead of == 'on'?



Yeah, it still doesn't work.

romaldus 9/15/2017

Your code:

[left]

ctrlSAME_AS_ABOVE('change', function(e)


Correct code:
[left] [/left][left] [/left][left]

[/left][left]

[/left][/left]

romaldus 9/15/2017


var ctrlSAME_AS_ABOVE = Runner.getControl(pageid, 'SAME_AS_ABOVE');

var ctrlLAST_NAME = Runner.getControl(pageid, 'LAST_NAME');

var ctrlFIRST_NAME = Runner.getControl(pageid, 'FIRST_NAME');

var ctrlCONTACT_LAST_NAME = Runner.getControl(pageid, 'CONTACT_LAST_NAME');

var ctrlCONTACT_FIRST_NAME = Runner.getControl(pageid, 'CONTACT_FIRST_NAME');
ctrlSAME_AS_ABOVE.on('change', function(e)

{

if(this.getValue() == '1')

{

ctrlLAST_NAME.setValue (ctrlCONTACT_LAST_NAME.getValue());

ctrlFIRST_NAME.setValue (ctrlCONTACT_FIRST_NAME.getValue());

}

});
I
i.NoLim author 9/15/2017




var ctrlSAME_AS_ABOVE = Runner.getControl(pageid, 'SAME_AS_ABOVE');

var ctrlLAST_NAME = Runner.getControl(pageid, 'LAST_NAME');

var ctrlFIRST_NAME = Runner.getControl(pageid, 'FIRST_NAME');

var ctrlCONTACT_LAST_NAME = Runner.getControl(pageid, 'CONTACT_LAST_NAME');

var ctrlCONTACT_FIRST_NAME = Runner.getControl(pageid, 'CONTACT_FIRST_NAME');
ctrlSAME_AS_ABOVE.on('change', function(e)

{

if(this.getValue() == '1')

{

ctrlLAST_NAME.setValue (ctrlCONTACT_LAST_NAME.getValue());

ctrlFIRST_NAME.setValue (ctrlCONTACT_FIRST_NAME.getValue());

}

});



Thank you for your reply. I made the correction but unfortunately it still doesn't work. Just for testing purposes, I decided to make it so when the checkbox is selected the emergency fields are hidden and when is not they are shown. By default the checkbox is unchecked, if I check it the fields are hidden but if I uncheck it the fields do not reappear. I'm not sure if this is helpful in any way to resolve my issue.
Also, I made a mistake when typing my code above. I used LAST_NAME instead of EMERGENCY_LAST_NAME and FIRST_NAME instead of EMERGENCY_FIRST_NAME. This has been fixed in my code as well, it still doesn't work though. Current code:

var ctrlSAME_AS_ABOVE = Runner.getControl(pageid, 'SAME_AS_ABOVE');

var ctrlEMERGENCY_LAST_NAME = Runner.getControl(pageid, 'EMERGENCY_LAST_NAME');

var ctrlEMERGENCY_FIRST_NAME = Runner.getControl(pageid, 'EMERGENCY_FIRST_NAME');

var ctrlCONTACT_LAST_NAME = Runner.getControl(pageid, 'CONTACT_LAST_NAME');

var ctrlCONTACT_FIRST_NAME = Runner.getControl(pageid, 'CONTACT_FIRST_NAME');
ctrlSAME_AS_ABOVE.on('change', function(e)

{

   if(this.getValue() == '1')

   {

ctrlEMERGENCY_LAST_NAME.setValue(ctrlCONTACT_LAST_NAME.getValue());

      ctrlEMERGENCY_FIRST_NAME.setValue(ctrlCONTACT_FIRST_NAME.getValue());

   }

});
jadachDevClub member 9/15/2017

First, make your EMERGENCY_LAST_NAME and EMERGENCY_FIRST_NAME required in the field properties.
Then do this:
On your add page javascript onload event use:

var ctrlSAME_AS_ABOVE = Runner.getControl(pageid, 'SAME_AS_ABOVE');

var ctrlEMERGENCY_LAST_NAME = Runner.getControl(pageid, 'EMERGENCY_LAST_NAME');

var ctrlEMERGENCY_FIRST_NAME = Runner.getControl(pageid, 'EMERGENCY_FIRST_NAME');
ctrlSAME_AS_ABOVE.on('change', function(e){

if (this.getValue() == 'on'){

pageObj.hideField("EMERGENCY_LAST_NAME");

pageObj.hideField("EMERGENCY_FIRST_NAME");

ctrlEMERGENCY_LAST_NAME.removeValidation("IsRequired");

ctrlEMERGENCY_FIRST_NAME.removeValidation("IsRequired");

ctrlEMERGENCY_LAST_NAME.setValue('');

ctrlEMERGENCY_FIRST_NAME.setValue('');

}

else{

pageObj.showField("EMERGENCY_LAST_NAME");

pageObj.showField("EMERGENCY_FIRST_NAME");

ctrlEMERGENCY_LAST_NAME.addValidation("IsRequired");

ctrlEMERGENCY_FIRST_NAME.addValidation("IsRequired");

}

});


On your before record added event use:

if (values["SAME_AS_ABOVE"] == "1")

{

values["EMERGENCY_LAST_NAME"] = values["CONTACT_LAST_NAME"];

values["EMERGENCY_FIRST_NAME"] = values["CONTACT_FIRST_NAME"];

}

else

{

// do nothing

}


I am assuming your checkbox is a bit field.

I
i.NoLim author 9/15/2017



First, make your EMERGENCY_LAST_NAME and EMERGENCY_FIRST_NAME required in the field properties.
Then do this:
On your add page javascript onload event use:

var ctrlSAME_AS_ABOVE = Runner.getControl(pageid, 'SAME_AS_ABOVE');

var ctrlEMERGENCY_LAST_NAME = Runner.getControl(pageid, 'EMERGENCY_LAST_NAME');

var ctrlEMERGENCY_FIRST_NAME = Runner.getControl(pageid, 'EMERGENCY_FIRST_NAME');
ctrlSAME_AS_ABOVE.on('change', function(e){

if (this.getValue() == 'on'){

pageObj.hideField("EMERGENCY_LAST_NAME");

pageObj.hideField("EMERGENCY_FIRST_NAME");

ctrlEMERGENCY_LAST_NAME.removeValidation("IsRequired");

ctrlEMERGENCY_FIRST_NAME.removeValidation("IsRequired");

ctrlEMERGENCY_LAST_NAME.setValue('');

ctrlEMERGENCY_FIRST_NAME.setValue('');

}

else{

pageObj.showField("EMERGENCY_LAST_NAME");

pageObj.showField("EMERGENCY_FIRST_NAME");

ctrlEMERGENCY_LAST_NAME.addValidation("IsRequired");

ctrlEMERGENCY_FIRST_NAME.addValidation("IsRequired");

}

});


On your before record added event use:

if (values["SAME_AS_ABOVE"] == "1")

{

values["EMERGENCY_LAST_NAME"] = values["CONTACT_LAST_NAME"];

values["EMERGENCY_FIRST_NAME"] = values["CONTACT_FIRST_NAME"];

}

else

{

// do nothing

}


I am assuming your checkbox is a bit field.


Thank you for your response.
I haven't tried it yet but I understand what you did. There is another field, EMERGENCY_PHONE_NUMBER, that I didn't include in my question because I thought it would just follow the same code as EMERGENCY_LAST_NAME and EMERGENCY_FIRST_NAME. There are times when the names will be the same but a different phone number will be given. If I use the solution you provided the user will not be able to edit the phone number because it would be hidden. I guess I can just leave EMERGENCY_PHONE_NUMBER visible and not write on that field in the "Before Record Added" event.

jadachDevClub member 9/16/2017



Thank you for your response.
I haven't tried it yet but I understand what you did. There is another field, EMERGENCY_PHONE_NUMBER, that I didn't include in my question because I thought it would just follow the same code as EMERGENCY_LAST_NAME and EMERGENCY_FIRST_NAME. There are times when the names will be the same but a different phone number will be given. If I use the solution you provided the user will not be able to edit the phone number because it would be hidden. I guess I can just leave EMERGENCY_PHONE_NUMBER visible and not write on that field in the "Before Record Added" event.


OK, then do this:

var ctrlSAME_AS_ABOVE = Runner.getControl(pageid, 'SAME_AS_ABOVE');

var ctrlEMERGENCY_LAST_NAME = Runner.getControl(pageid, 'EMERGENCY_LAST_NAME');

var ctrlEMERGENCY_FIRST_NAME = Runner.getControl(pageid, 'EMERGENCY_FIRST_NAME');

var ctrlCONTACT_LAST_NAME = Runner.getControl(pageid, 'CONTACT_LAST_NAME');

var ctrlCONTACT_FIRST_NAME = Runner.getControl(pageid, 'CONTACT_FIRST_NAME');
ctrlSAME_AS_ABOVE.on('change', function(e){

if (this.getValue() == 'on'){

ctrlEMERGENCY_LAST_NAME.setValue(ctrlCONTACT_LAST_NAME.getValue());

ctrlEMERGENCY_FIRST_NAME.setValue(ctrlCONTACT_FIRST_NAME.getValue());

}

else{

ctrlEMERGENCY_LAST_NAME.setValue('');

ctrlEMERGENCY_FIRST_NAME.setValue('');

}

});
I
i.NoLim author 9/18/2017



OK, then do this:

var ctrlSAME_AS_ABOVE = Runner.getControl(pageid, 'SAME_AS_ABOVE');

var ctrlEMERGENCY_LAST_NAME = Runner.getControl(pageid, 'EMERGENCY_LAST_NAME');

var ctrlEMERGENCY_FIRST_NAME = Runner.getControl(pageid, 'EMERGENCY_FIRST_NAME');

var ctrlCONTACT_LAST_NAME = Runner.getControl(pageid, 'CONTACT_LAST_NAME');

var ctrlCONTACT_FIRST_NAME = Runner.getControl(pageid, 'CONTACT_FIRST_NAME');
ctrlSAME_AS_ABOVE.on('change', function(e){

if (this.getValue() == 'on'){

ctrlEMERGENCY_LAST_NAME.setValue(ctrlCONTACT_LAST_NAME.getValue());

ctrlEMERGENCY_FIRST_NAME.setValue(ctrlCONTACT_FIRST_NAME.getValue());

}

else{

ctrlEMERGENCY_LAST_NAME.setValue('');

ctrlEMERGENCY_FIRST_NAME.setValue('');

}

});




You are amazing! Thank you! I guess the reason mine wasn't working is because it was missing the else statement.
Thanks again.

C
chaintm 11/6/2017

hey all,
I used this thread to help myself as well, it works great! however, I don't care for the fact that when I check box the check box on or off , it auto updates... I did the following and tried changing the getvalue()=='on' to '1' and that didn't work as well , any thoughts? I am not a huge javascript guy, but thank god you guys are <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=83654&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' /> I am all database and php <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=83654&image=2&table=forumreplies' class='bbc_emoticon' alt=';)' /> here is my code...

var ctrlFieldc = Runner.getControl(pageid, 'checkbox');

var ctrlFielda = Runner.getControl(pageid, 'Cust_Return');

var ctrlField = Runner.getControl(pageid, 'Billto_company');

var ctrlField1 = Runner.getControl(pageid, 'Billto_name');

var ctrlField2 = Runner.getControl(pageid, 'Billto_Lname');

var ctrlField3 = Runner.getControl(pageid, 'Billto_address');

var ctrlField4 = Runner.getControl(pageid, 'Billto_city');

var ctrlField5 = Runner.getControl(pageid, 'Billto_state');

var ctrlField6 = Runner.getControl(pageid, 'Billto_zip');

var ctrlFieldd = Runner.getControl(pageid, 'Billto_apt');
var ctrlFieldb = Runner.getControl(pageid, 'Cust_Return2');

var ctrlField7 = Runner.getControl(pageid, 'Shipto_company');

var ctrlField8 = Runner.getControl(pageid, 'Shipto_name');

var ctrlField9 = Runner.getControl(pageid, 'Shipto_Lname');

var ctrlField10 = Runner.getControl(pageid, 'Shipto_address');

var ctrlField11 = Runner.getControl(pageid, 'Shipto_city');

var ctrlField12 = Runner.getControl(pageid, 'Shipto_state');

var ctrlField13 = Runner.getControl(pageid, 'Shipto_zip');

var ctrlField14 = Runner.getControl(pageid, 'Shipto_apt');
ctrlFieldc.on('change', function(e)

{

if (this.getValue() == 'on')

{

alert('Checked');

ctrlFieldb.setValue(ctrlFielda.getValue());

ctrlField7.setValue(ctrlField.getValue());

ctrlField8.setValue(ctrlField1.getValue());

ctrlField9.setValue(ctrlField2.getValue());

ctrlField10.setValue(ctrlField3.getValue());

ctrlField11.setValue(ctrlField4.getValue());

ctrlField12.setValue(ctrlField5.getValue());

ctrlField13.setValue(ctrlField6.getValue());

ctrlField14.setValue(ctrlField6.getValue());

}

else

{

ctrlFieldb.setValue(ctrlFielda.getValue(''));

ctrlField7.setValue(ctrlField.getValue(''));

ctrlField8.setValue(ctrlField1.getValue(''));

ctrlField9.setValue(ctrlField2.getValue(''));

ctrlField10.setValue(ctrlField3.getValue(''));

ctrlField11.setValue(ctrlField4.getValue(''));

ctrlField12.setValue(ctrlField5.getValue(''));

ctrlField13.setValue(ctrlField6.getValue(''));

ctrlField14.setValue(ctrlField6.getValue(''));

}

});


NM FIXED IT! <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=83654&image=3&table=forumreplies' class='bbc_emoticon' alt=':)' /> for others all you do is just remove the reduancy, IE do below...

var ctrlFieldc = Runner.getControl(pageid, 'checkbox');

var ctrlFielda = Runner.getControl(pageid, 'Cust_Return');

var ctrlField = Runner.getControl(pageid, 'Billto_company');

var ctrlField1 = Runner.getControl(pageid, 'Billto_name');

var ctrlField2 = Runner.getControl(pageid, 'Billto_Lname');

var ctrlField3 = Runner.getControl(pageid, 'Billto_address');

var ctrlField4 = Runner.getControl(pageid, 'Billto_city');

var ctrlField5 = Runner.getControl(pageid, 'Billto_state');

var ctrlField6 = Runner.getControl(pageid, 'Billto_zip');

var ctrlFieldd = Runner.getControl(pageid, 'Billto_apt');
var ctrlFieldb = Runner.getControl(pageid, 'Cust_Return2');

var ctrlField7 = Runner.getControl(pageid, 'Shipto_company');

var ctrlField8 = Runner.getControl(pageid, 'Shipto_name');

var ctrlField9 = Runner.getControl(pageid, 'Shipto_Lname');

var ctrlField10 = Runner.getControl(pageid, 'Shipto_address');

var ctrlField11 = Runner.getControl(pageid, 'Shipto_city');

var ctrlField12 = Runner.getControl(pageid, 'Shipto_state');

var ctrlField13 = Runner.getControl(pageid, 'Shipto_zip');

var ctrlField14 = Runner.getControl(pageid, 'Shipto_apt');
ctrlFieldc.on('change', function(e)

{

if (this.getValue() == 'on')

{

alert('Checked');

ctrlFieldb.setValue(ctrlFielda.getValue());

ctrlField7.setValue(ctrlField.getValue());

ctrlField8.setValue(ctrlField1.getValue());

ctrlField9.setValue(ctrlField2.getValue());

ctrlField10.setValue(ctrlField3.getValue());

ctrlField11.setValue(ctrlField4.getValue());

ctrlField12.setValue(ctrlField5.getValue());

ctrlField13.setValue(ctrlField6.getValue());

ctrlField14.setValue(ctrlField6.getValue());

}

else

{
}

});