AnsweredAssumed Answered

Help with Marketo form submission in the background code

Question asked by Alexandra Aghinitei on Mar 4, 2019
Latest reply on Mar 5, 2019 by Sanford Whiteman

Hi,

 

I'm very new to Marketo and not a developer and I know this topic has been covered a lot, but we're doing something wrong and we don't know what that is.

We're trying to create a form submission in the background to trigger campaigns based on form submits; we've embedded the empty form on a test LP, but it's not submitting anything to our Marketo. Can you guys please look over the code and tell me what we're doing wrong?

 

<script type="text/javascript" src="//app-ab34.marketo.com/js/forms2/js/forms2.min.js"></script> <form id="mktoForm_1038" style="display:none"></form> <script>     MktoForms2.loadForm("//app-ab34.marketo.com", "091-OYP-062", 1038);     setTimeout( function(){         if( typeof MktoForms2 != "undefined" ) {             MktoForms2.whenReady(function(mktoForm) {                  var customFormData = {                     formSelector: '#trial-form',                     fieldMap: [{                         marketo: 'FirstName',                         custom: '#form-field-firstname',                         marketo: 'LastName',                         custom: '#form-field-lastname',                         marketo: 'Email',                         custom: '#form-field-email',                         marketo: 'Company',                         custom: '#form-field-company',                         marketo: 'Country',                         custom: '#form-field-country',                         marketo: 'State',                         custom: '#form-field-state',                         marketo: 'City',                         custom: '#form-field-city',                         marketo: 'PostalCode',                         custom: '#form-field-postal',                         marketo: 'LeadSource',                         custom: '#form-field-leadsource'                      }]                 }                  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).vals({"web":"true"});                      // submit Marketo form                     mktoForm.submit();                      // stop custom HTML form submission                     e.preventDefault();                  });             });         }     }, 2000 ); // two-second delay </script>

 

<script type="text/javascript" src="//app-ab34.marketo.com/js/forms2/js/forms2.min.js"></script>

<form id="mktoForm_1038" style="display:none"></form>

<script>

    MktoForms2.loadForm("//app-ab34.marketo.com", "091-OYP-062", 1038);

    setTimeout( function(){

        if( typeof MktoForms2 != "undefined" ) {

            MktoForms2.whenReady(function(mktoForm) {

 

                var customFormData = {

                    formSelector: '#trial-form',

                    fieldMap: [{

                        marketo: 'FirstName',

                        custom: '#form-field-firstname',

                        marketo: 'LastName',

                        custom: '#form-field-lastname',

                        marketo: 'Email',

                        custom: '#form-field-email',

                        marketo: 'Company',

                        custom: '#form-field-company',

                        marketo: 'Country',

                        custom: '#form-field-country',

                        marketo: 'State',

                        custom: '#form-field-state',

                        marketo: 'City',

                        custom: '#form-field-city',

                        marketo: 'PostalCode',

                        custom: '#form-field-postal',

                        marketo: 'LeadSource',

                        custom: '#form-field-leadsource'

 

                    }]

                }

 

                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).vals({"web":"true"});

 

                    // submit Marketo form

                    mktoForm.submit();

 

                    // stop custom HTML form submission

                    e.preventDefault();

 

                });

            });

        }

    }, 2000 ); // two-second delay

</script>

Outcomes