Make a Marketo Form Submission in the background

Kenny_Elkington
Marketo Employee
Marketo Employee

This post originally appears on the Marketo Developers Blog

When your organization has many different platforms for hosting web content and customer data it becomes fairly common to need parallel submissions from a form so that the resulting data can be gathered in separate platforms. There are several strategies to do this, but the best one is often the simplest: Using the Forms 2 API to submit a hidden Marketo form. This will work with any new Marketo Form, but ideally you should create an empty form for this, which has no fields:

Empty Form

This will ensure that the form doesn’t load any more data than necessary, since we don’t need to render anything. Now just grab the embed code from your form and add it to the body of your desired page, making a small modification. You embed code includes a form element like this:

<form id="mktoForm_1068"></form>

You’ll want to add ‘style=”display:none”‘ to the element so it is not visible, like this:

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

Once the form is embedded and hidden, the code to submit the form is really quite simple:

var myForm = MktoForms2.allForms()[0]; 
myForm
.addHiddenFields({
     //These are the values which will be submitted to Marketo
     "Email":"test@example.com",
     "FirstName":"John",
     "LastName":"Doe" });
myForm
.submit();

Forms submitted this way will behave exactly like if the lead had filled out and submitted a visible form. Triggering the submission will vary between implementations since each one will have a different action to prompt it, but you can make it occur on basically any action. The important part is setting your fields and values correctly. Be sure to use the SOAP API name of your fields which you can find with Export Field Names to esnure correct submission of values.

27253
65
65 Comments
SanfordWhiteman
Level 10 - Community Moderator

Screenshots are hard to troubleshoot.   What's the actual URL?

Anonymous
Not applicable

Sorry, that's true Sanford Whiteman

I've put together 3 examples.

Example 1http://insights.demo.squiz.net/university/test-page/test-page

Just plain code as per the example above.

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

<form id="mktoForm_2" style="display:none;"></form>

<script>

  MktoForms2.loadForm("//app-sn01.marketo.com", "466-ZFM-936", 2);

  var marketoForm = MktoForms2.allForms()[0];

  marketoForm.addHiddenFields({

     //These are the values which are submitted to Marketo

  "Email": "email6@test.com",

  "FirstName": "Email",

  "LastName": "Test Form 6"

  });

  marketoForm.submit();

</script>

It is not working because it's throwing the error in the console:

Uncaught TypeError: Cannot read property 'addHiddenFields' of undefined

It looks like the variable "marketoForm" is undefined for some reason and that's why the "addHiddenFields" function is not working. But I have no idea why that variable get undefined after calling MktoForms2.allForms()[0];

Example 2 - http://insights.demo.squiz.net/university/test-page/another-test-page

I added a simple html form and on submit, I submit to Marketo. And it works!

<form id="mktoForm_2" style="display:none;"></form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

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

<script type="text/javascript">

  MktoForms2.loadForm("//app-sn01.marketo.com", "466-ZFM-936", 2);

  $('#test-form').submit(function( event ) {

  

  var marketoForm = MktoForms2.allForms()[0];

  marketoForm.addHiddenFields({

      //These are the values which will be submitted to Marketo

      "Email": $('#email').val(),

      "FirstName": $('#name').val(),

      "LastName": $('#surname').val()

  });

  marketoForm.submit();

  });

</script>

Example 3 - http://insights.demo.squiz.net/university/test-page/the-last-test-page

Same as above but instead adding the hidden fields to the Marketo form using jQuery to grab the field values, I hard code the values directly. And it also works!

<form id="mktoForm_2" style="display:none;"></form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

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

<script type="text/javascript">

MktoForms2.loadForm("//app-sn01.marketo.com", "466-ZFM-936", 2);

$('#test-form').submit(function( event ) {

  

  var marketoForm = MktoForms2.allForms()[0];

  marketoForm.addHiddenFields({

      //These are the values which will be submitted to Marketo

      "Email": "email5@test.com",

      "FirstName": "Email",

      "LastName": "Test Form 5"

  });

  marketoForm.submit();

  });

</script>

So I don't understand what difference having a jquery event like "on submit" makes to the original code...I am a bit lost to be honest.

The issue is that due to our current requirements, I need to submit the Marketo form in the background in the "Thank you " page of the original form in the CMS.

Thanks for your help, really appreciated.

SanfordWhiteman
Level 10 - Community Moderator

Classic async programming error!  (I love this stuff.)

The simple reason 1 doesn't work is that MktoForms2.loadForm() loads the form descriptor asynchronously.  Thus the form is not going to be in the MktoForms2.allForms() array on the very next line (when the descriptor hasn't even started to be downloaded).

Use the onReady/whenReady listener:

MktoForms2.loadForm("//app-sn01.marketo.com", "466-ZFM-936", 2);

MktoForms2.whenReady(function(form){

form.addHiddenFields({

     //These are the values which are submitted to Marketo

  "Email": "email6@test.com",

  "FirstName": "Email",

  "LastName": "Test Form 6"

  });

  form.submit();

});

Anonymous
Not applicable

Thanks so much Sanford Whiteman​. Really appreciated.

Is there a way to stop the page to reload all the time?

The form submission is forcing the page to reload which submits the form again and again.

I tried adding an event.preventDefault() with no luck.

SanfordWhiteman
Level 10 - Community Moderator

MktoForms2.loadForm("//app-sn01.marketo.com", "466-ZFM-936", 2);

MktoForms2.whenReady(function(form){

form.onSuccess(function(vals,tyURL){

  return false;

});

form.addHiddenFields({

     //These are the values which are submitted to Marketo

  "Email": "email6@test.com",

  "FirstName": "Email",

  "LastName": "Test Form 6"

  });

  form.submit();

});

Anonymous
Not applicable

Awesome! Thanks Sanford Whiteman

By the way, do you know if there is any formal documentation on all this? Like for example in all the different functions available for the Marketo form objects such as:

SanfordWhiteman
Level 10 - Community Moderator

Several are private methods that aren't usable outside of the form scope. I know how to use most of the others, but there's no formal documentation other than the Forms 2.0 docs.  If there's some specific thing you're trying to do, let me know.

Cecile_Maindron
Level 10

Hello,

I have created a hidden form and I have passed info to our web developer. He has followed instructions but it doesn't work and he has no idea what needs to be corrected. Can you help.

He has inserted the following script:

<script src="//app-abk.marketo.com/js/forms2/js/forms2.min.js"></script>
<form id="mktoForm_4392" style="display:none"></form>
<script>MktoForms2.loadForm("//app-abk.marketo.com", "347-IAT-677", 4392);</script>
<script>
jQuery(document).ready(function(){
var myForm = MktoForms2.allForms()[0];
myForm.addHiddenFields(

{ //These are the values which will be submitted to Marketo "utm_content":"NULL", "utm_creative":"NULL", "utm_medium":"NULL", "utm_source":"NULL", "utm_term":"NULL", "utm_campaign":"NULL" }

);
myForm.submit();
});
</script>

but he is getting the following error message:

"Cannot read property 'addHiddenFields' of undefined"

Kenny_Elkington
Marketo Employee

Change this block:

<script>
jQuery(document).ready(function(){
var myForm = MktoForms2.allForms()[0];
myForm.addHiddenFields(

{ //These are the values which will be submitted to Marketo "utm_content":"NULL", "utm_creative":"NULL", "utm_medium":"NULL", "utm_source":"NULL", "utm_term":"NULL", "utm_campaign":"NULL" }

);
myForm.submit();
});
</script>

To this:

<script>

MktoForms2.whenReady(function(form){

form.addHiddenFields(

{ //These are the values which will be submitted to Marketo

"utm_content":"NULL", "utm_creative":"NULL", "utm_medium":"NULL", "utm_source":"NULL", "utm_term":"NULL", "utm_campaign":"NULL" }

);
form.submit();

});

</script>

Cecile_Maindron
Level 10

Thanks Kenny Elkington

We have changed code and the error is gone. However, the page refreshes every 30 seconds or so. It looks like the form is being submitted over and over. Maybe MktoForms2.whenReady is triggering the form to submit over and over. Or is this the expected behavior?