SOLVED

Check hidden field value before assigning a value.

Go to solution
Anonymous
Not applicable

Check hidden field value before assigning a value.

We want to use a master form for landing pages hosted on our own site.  We're able to do this and assign campaign ID, assets and the redirect url as needed with the following code:

<script src="//app-sj05.marketo.com/js/forms2/js/forms2.min.js"></script>

<form id="mktoForm_ourFormID"></form>

<script>

  MktoForms2.loadForm("//app-sj05.marketo.com", "ourID", ourFormID, function(form) {

  // Set values in hidden fields to determine campaign and asset to serve

  form.vals({

  "Campaign_Id__c":"assignedCampaignID",

  "Campaign_Asset__c":"assignedCampaignAsset"

  });

  // Override master redirect URL set in Marketo

  form.onSuccess(function(values, followUpUrl) {

  location.href = "newRedirectURL";

  // Return false to prevent the submission handler continuing with its own processing

  return false;

  });

  });

</script>

The next step is to assign the campaign iD only if it's not already included in the referring url.  So I need some sort of if/then statement that looks at the hidden campaign ID to see if there's a value - and assign a value only if there is not.  I'm not a javascript master by any stretch, and I'm not sure what is the best method for the Marketo API.  Can someone help me with this last bit of if/else?

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Check hidden field value before assigning a value.

The code will never work like that.  You would see an error if you looked at your console.

form.setValues({

     "Campaign_Asset__c":"HallmarkAssessTemplate"

});

if( !form.getValues()['Campaign_Id__c'] ) {

     form.setValues({

          "Campaign_Id__c":"701400000014xJ8"

     })

}

View solution in original post

4 REPLIES 4
SanfordWhiteman
Level 10 - Community Moderator

Re: Check hidden field value before assigning a value.

if( !form.getValues()['Campaign_Id__c'] ) {

     // field does not current have a value

}

Anonymous
Not applicable

Re: Check hidden field value before assigning a value.

I tried as so, but no luck:

<script src="//app-sj05.marketo.com/js/forms2/js/forms2.min.js"></script>

<form id="mktoForm_1491"></form>

<script>

  MktoForms2.loadForm("//app-sj05.marketo.com", "090-PRB-274", 1491, function(form) {

  form.vals({

                        // Set value in hidden field to determine asset to serve

                       "Campaign_Asset__c":"HallmarkAssessTemplate",

                        // Check for referring url campaign info, if blank set value in hidden field to determine campaign to serve

                       if( !form.getValues()['Campaign_Id__c'] ) {

                             "Campaign_Id__c":"701400000014xJ8"

                       }

                });

  // Override redirect URL set in Marketo

  form.onSuccess(function(values, followUpUrl) {

  location.href = "http://convercent.staging.wpengine.com/lp/ty-compliance-program-hallmarks-assessment-template";

  // Return false to prevent the submission handler continuing with its own processing

  return false;

  });

  });

</script>

Maybe I'm not putting your script in the right place?

SanfordWhiteman
Level 10 - Community Moderator

Re: Check hidden field value before assigning a value.

The code will never work like that.  You would see an error if you looked at your console.

form.setValues({

     "Campaign_Asset__c":"HallmarkAssessTemplate"

});

if( !form.getValues()['Campaign_Id__c'] ) {

     form.setValues({

          "Campaign_Id__c":"701400000014xJ8"

     })

}

Anonymous
Not applicable

Re: Check hidden field value before assigning a value.

I need to get better with the console...  This worked beautifully.  Thank you!