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;
});
});