Dynamically choosing a form won't record form fill

Robb_Barrett
Level 10

Dynamically choosing a form won't record form fill

Hi there -

I wrote some code to dynamically choose the form that is displayed. For example, if you recently filled out a form then on your next visit I present a shorter form that doesn't put name, email, etc on it.

When I run the page it works almost perfectly. The page loads, the form is selected and when clicked it does the appropriate follow up action. When I check the log for the record, the form fill is not recorded and the data values don't change.

Here are the relevant details of the code:

<script type="text/html" id="call-form"></script>

<div class="right-side-container">

                          <div class="right-side-top" id="right-side-top"> </div><!--right-side-top-->

                         <div id="thumbnaildiv" class="thumbnail"></div> <!--thumbnail-->     

                         <div class="this-form" id="this-form"></div><!--this-form-->

                      </div><!--right-side-container-->

<script>

  var knownForm = "{{my.Form ID - Known}}";

  var unknownForm = "{{my.Form ID - Unknown}}";

function pickForm() {

  if ((getFormPassCookie("_mkto_trk") !="") && ("{{lead.First Name}}" != "") && ("{{lead.Email Address}}" != "")) {

    return knownForm;

  } else {

    return unknownForm;

  }

}

var formName = 'mktoForm_'+pickForm();

var formCode = '<form id="'+formName+'"></form>';

document.getElementById("call-form").innerHTML =formCode;

document.getElementById("thumbnaildiv").style.display = "none";

    document.getElementById("right-side-top").innerHTML = document.getElementById("pre-form").innerHTML;

    document.getElementById("this-form").innerHTML = document.getElementById("call-form").innerHTML;

    MktoForms2.loadForm("//app-sj02.marketo.com", "XXX-XXX-XXX", pickForm(),

      function(form) {

      form.vals({"PostalCode":"{{lead.Postal Code}}",

                 "Phone":"{{lead.Phone Number}}",

                 "FirstName":"{{lead.First Name}}",

                 "LastName":"{{lead.Last Name}}",

                 "Company":"{{company.Company Name}}",

                 "Email":"{{lead.Email Address}}",

                 "City":"{{lead.City}}",

                 "Title":"{{lead.Job Title}}",  

                 "web_capture_state":"{{lead.State}}",

                 "Address":"{{lead.Address}}"});

      var formEl = form.getFormElem()[0];

      form.onSuccess(function() {

        setFormPassCookie(val, "true", {{my.Cookie-Days}});

        if (checkForDoc(LinkURL) != true) {

        window.location.assign(LinkURL);

        } else {  

        document.getElementById(formName).style.display="none";

        document.getElementById("right-side-top").innerHTML = postForm;

        document.getElementById("thumbnaildiv").style.display="block";

        document.getElementById("thumbnaildiv").innerHTML = button;

        }

  return false;

      });

    });

4 REPLIES 4
Robb_Barrett
Level 10

Re: Dynamically choosing a form won't record form fill

Forget this. I found that there's another issue causing problems.

Anonymous
Not applicable

Re: Dynamically choosing a form won't record form fill

Robb Barrett UAT​ Did you end up findng the issue? We are using a form and see its not being logged as a form fill in the activity record of the lead and the form name is coming up empty. Were you seeing that behavior?

Robb_Barrett
Level 10

Re: Dynamically choosing a form won't record form fill

There could be a couple issues.  For one, in the code above I tried using JS to create the form. That didn't work, so i put in tokens for the form ID and it worked better.  That code is a little out of date, but here's the essence of the change.

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

<script type="text/html" id="call-form"><form id="mktoForm_{{my.Form ID}}"></form></script>

Next, I realized that if you send an email and click the link and fill out the form with a new email address, it'll create a new record. The email, click and web visit will be credited to the original email address but the form fill will be credited to the new record.

Post your code and I'll see if I can help troubleshoot.

SanfordWhiteman
Level 10 - Community Moderator

Re: Dynamically choosing a form won't record form fill

Did you end up findng the issue? We are using a form and see its not being logged as a form fill in the activity record of the lead and the form name is coming up empty. Were you seeing that behavior?

"Using a form" isn't the only thing what Robb is doing.  He's dynamically choosing a form ID in the browser and then embedding it (incidentally, this does work with pure JS -- I do it all the time -- but of course will also work with a token in the markup).

What exactly do you mean by "using a form"?  And where does the form fill appear (doing a smart list for Filled Out Form will show you who is being credited with the activity)? Please post some code and/or an example URL, and more details.