Conditional Progressive Form Substitution?
I'm trying to create a form where if the user selects a certain value from a dropdown field, their information will be sent when they click Submit, but then a second form with appear with additional optional questions. If they select anything else, they'll just get redirected to a Thank You page.
Note: I've had the discussion with the team about using dynamic fields and just having the optional questions appear at the bottom of the first form if they meet the criteria, but since it's already a pretty long form, they think it will lead to greater form abandonment and would rather capture the required information first.
I have this working in pieces - when I load the first form the traditional way, the conditional logic works where the form is hidden upon Submit (prepping for the next form) or redirects. I also tried the code here (https://codepen.io/figureone/pen/wpXrvx?editors=1010) which is working correctly to load the second form, however it loads for everyone. I just need to figure out how to marry the two together and that's where I'm running into trouble.
Here is the code I'm using when it is just one form:
<script>
function loadMktoForm() {
( ( $ ) => {
let criteriaMet = false;
MktoForms2.loadForm(
domain_name,
munchkin_id,
form_id,
( form ) => {
form.onSubmit( () => {
// IF ORGANIZATION TYPE MEETS CRITERIA, SET VARIABLE TO CONTROL WHAT THE SUBMIT BUTTON WILL DO
var field_val = form.getValues()['Organization_Type__c'];
if ( (field_val === 'Medical Practice') || (field_val === 'Health System') ) {
criteriaMet = true;
}
} );
// Add an onSuccess handler
form.onSuccess( () => { // values, followUpUrl
if ( criteriaMet === true ) {
form.getFormElem().hide();
}
else {
// Take the lead to a different page on successful submit, ignoring the form's configured followUpUrl
location.href = "/thank-you/";
}
// Return false to prevent the submission handler continuing with its own processing
return false;
} );
}
);
} )( jQuery );
}
// Init the marketo form on page load
loadMktoForm();
</script>
Thank you!