Make a Marketo Form Submission in the background | Community
Skip to main content
Kenny_Elkington
New Participant
September 30, 2015

Make a Marketo Form Submission in the background

  • September 30, 2015
  • 65 replies
  • 35403 views

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.

65 replies

Cecile_Maindron
New Participant
July 5, 2016

@Sanford Whiteman​ Thanks again for your prompt answer. I have some trouble following you. I thought the custom .html was used so that known leads don't have to submit form. In my case I want the form to be submitted by known leads and not submitted by anonymous leads to avoid the creation of net new leads with 0 data.

So if I add the Forms 2.0 Known Lead HTML feature how can it work?

Or do you mean that my web dev. should use the feature to determine if lead is known and then apply the following logic : if feature is activated = lead is known = submit form?

SanfordWhiteman
New Participant
July 4, 2016

Then you want to only conditionally run form.submit() if

  1. the lead is already known; or
  2. the lead is becoming known during this form post (i.e. you are osting their email = identifying info)

You can take advantage of the Forms 2.0 Known Lead HTML feature to determine if the lead is already known.  If the KL HTML is rendered, then the lead is known.  Conversely, if the KL HTML is not rendered, the lead is not already known, but if the Email field is not filled in you should not submit the form because it's just going to create the clutter you're mentioning.

The code your dev added to stop the form from submitting repeatedly is also broken.  Plus I don't get the waiting 5 seconds to submit the form; that's just going to mean you lose the form post if someone goes to another page within 5 seconds.  Why would you want to do that?

Cecile_Maindron
New Participant
July 4, 2016

@Sanford Whiteman@Kenny Elkington

Huge thanks for your support. This is working, you rock! I just have another issue and it doesn't look like this has already been addressed:

Marketo is automatically transforming all the anonymous activities on my TY pages (e.g. www.talend.com/download/thankyou/mdm) - where hidden form are hosted - into new leads. I believe that this is due to fact that there is a submitted form, even if form is hidden. How can I prevent this from happening?

Cecile_Maindron
New Participant
June 9, 2016

thanks! hope our web dev. will know what to do next....

SanfordWhiteman
New Participant
June 8, 2016

.whenReady will trigger whenever a form is ready for use; it will not by itself create a loop.  But if you already are redrawing the form after submission, then you will have a loop, since every time it redraws whenReady will cause it to resubmit, over and over.

Cecile_Maindron
New Participant
June 8, 2016

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?

Kenny_Elkington
New Participant
June 6, 2016

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
New Participant
June 6, 2016

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"

SanfordWhiteman
New Participant
March 11, 2016

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.

March 11, 2016

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: