Skip to main content
Level 1
Aug 17, 2021 11:31 PM UTC
Solved

Submit a hidden Marketo form with 2 forms on page

Hello (@Sanford  😀)

 

I have a use case where we have two site forms on the same page and need them to submit different Marketo forms in the background. I have put together a codepen to test it out, but when I submit either form both submissions go through. I figure that we need to combine both the background submission and two forms on the page but cannot get the code to mesh together.

 

Any help is much appreciated.

 

Best,

Thomas

SanfordWhiteman
SanfordWhitemanAccepted solution
User Group Leader
Aug 19, 2021 5:24 AM UTC

Because whenReady fires for every form that’s ready, and you’re not doing any further filtering by ID.

 

You want only one whenReady listener:

MktoForms2.loadForm("//go.imply.io", "910-OTN-223", 1904); MktoForms2.loadForm("//go.imply.io", "910-OTN-223", 1903); MktoForms2.whenReady(function (mktoForm) { var customFormDataByMarketoFormId = { 1904 : { formSelector: "#download-form", fieldMap: [ { marketo: "Email", custom: "#download-email" }, { marketo: "FirstName", custom: "#download-first-name" }, { marketo: "LastName", custom: "#download-last-name" }, { marketo: "Company", custom: "#download-organization" } ] }, 1903 : { formSelector: "#cloud-signup-form", fieldMap: [ { marketo: "Email", custom: "#signup-email" }, { marketo: "FirstName", custom: "#signup-first-name" }, { marketo: "LastName", custom: "#signup-last-name" }, { marketo: "Company", custom: "#signup-organization" } ] } }; var customFormData = customFormDataByMarketoFormId[mktoForm.getId()]; document .querySelector(customFormData.formSelector) .addEventListener("submit", function (e) { var customForm = e.target, mktoFields = {}; // iterate over fields on custom form to create MktoForms-compat object customFormData.fieldMap.forEach(function (field) { mktoFields[field.marketo] = customForm.querySelector(field.custom).value; }); // add to Marketo form mktoForm.addHiddenFields(mktoFields); // submit Marketo form mktoForm.submit(); // stop custom HTML form submission e.preventDefault(); }); });

 

3 replies

Adobe Employee
Aug 18, 2021 12:45 AM UTC

@thomas-nomad 

I am definitely not @sanfordwhiteman.

But highlighting his solution to the same problem you are facing

 

Translate
SanfordWhiteman
User Group Leader
SanfordWhitemanUser Group LeaderAccepted solution
User Group Leader
Aug 19, 2021 5:24 AM UTC

Because whenReady fires for every form that’s ready, and you’re not doing any further filtering by ID.

 

You want only one whenReady listener:

MktoForms2.loadForm("//go.imply.io", "910-OTN-223", 1904); MktoForms2.loadForm("//go.imply.io", "910-OTN-223", 1903); MktoForms2.whenReady(function (mktoForm) { var customFormDataByMarketoFormId = { 1904 : { formSelector: "#download-form", fieldMap: [ { marketo: "Email", custom: "#download-email" }, { marketo: "FirstName", custom: "#download-first-name" }, { marketo: "LastName", custom: "#download-last-name" }, { marketo: "Company", custom: "#download-organization" } ] }, 1903 : { formSelector: "#cloud-signup-form", fieldMap: [ { marketo: "Email", custom: "#signup-email" }, { marketo: "FirstName", custom: "#signup-first-name" }, { marketo: "LastName", custom: "#signup-last-name" }, { marketo: "Company", custom: "#signup-organization" } ] } }; var customFormData = customFormDataByMarketoFormId[mktoForm.getId()]; document .querySelector(customFormData.formSelector) .addEventListener("submit", function (e) { var customForm = e.target, mktoFields = {}; // iterate over fields on custom form to create MktoForms-compat object customFormData.fieldMap.forEach(function (field) { mktoFields[field.marketo] = customForm.querySelector(field.custom).value; }); // add to Marketo form mktoForm.addHiddenFields(mktoFields); // submit Marketo form mktoForm.submit(); // stop custom HTML form submission e.preventDefault(); }); });

 

Translate
Level 1
Aug 23, 2021 8:16 PM UTC

Thanks @sanfordwhiteman . Works perfectly.

Translate