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

SanfordWhiteman
New Participant
January 11, 2018

You don't need code to do what you describe, nor do you need a background form post.

If you "already have the information," in Marketo terms we say ​their Munchkin session is already associated​.

​In Form Editor » Form Settings » Settings, you can choose the option ​If Known Visitor, Show Custom HTML ​("KV HTML" for short). This allows you to show a shortened form with only the CTA button if the session is associated. The button click is logged as a Filled Out Form activity.

Note you can also use a dedicated form with the Submit button only, no fields. The difference from KV HTML is it won't include the "Not You?" link.

If you have more questions about this, please open a new thread in Products​ (discussions under a blog post aren't well-monitored and aren't useful for later searches).

January 11, 2018

Hello All,


I am fairly new to Marketo and I came across this post as I am looking to send people from an email campaign to a landing page to get them to submit a request. However, I don't need them to complete a form again as I already have their information on the database and it just adds unnecessary steps to the user journey.

The idea to capture their information in the background behind the scenes allowing a single action of clicking/submitting the CTA is great.

However, I am not a programmer or coder although I have some understanding of what was mentioned above, I need help with the script to get it to work.

Can anyone who has done this before help me please?

Thanks,

Jack

SanfordWhiteman
New Participant
October 2, 2017

You can't use code like this. The call to loadForm loads the form descriptor (metadata) asynchronously and then injects the form object into the page when it's done.

Thus you can't just access the MktoForms2.allForms() collection directly after loadForm() because the form doesn't exist yet.

It's a basic reality of JS. You have to use the Forms 2.0 event model.

Please open a thread in Products and post your code using syntax highlighting in the Advanced Editor:

and I'll show you how it's done.

Anna_Blanchet1
New Participant
October 2, 2017

Hi,

We're attempting to make a form submission in the background. I've created the embedded code but our IT staff keeps getting an error. Below is there feedback. I am not a coder but would greatly appreciate any feedback that I can pass along to our developers..  Thanks!

--

loadForm method is returning the object with all forms, but “var myForm = MktoForms2.allForms()[0];” is returning “undefined”.

<head>

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

</head>

<script src="https://app-sj04.marketo.com/js/forms2/js/forms2.min.js"></script>

<html>

<form id="mktoForm_1255">

Just Checking

</form>

</html>

<script>

var forms = MktoForms2.loadForm("https//app-sj04.marketo.com", "903-EIZ-879", 1255);

  1. console.log(forms);

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

  1. console.log(myForm);
  2. myForm.addHiddenFields({

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

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

"FirstName":"John",

"LastName":"Doe"

});

  1. myForm.submit();

//mktoForm_

</script>

SanfordWhiteman
New Participant
June 14, 2017

When you only

  return false;

from your onSuccess listener function, then the form submits, from onSuccess, then nothing else would be expected to happen. In fact the (Marketo) form does get posted in the background -- but nothing else happens on the page, so you wouldn't know that without looking in Dev Tools and in your Marketo database.

To replace the form contents, add elements/innerHTML before returning false.

Observe: Marketo Form Submit - JSFiddle

June 12, 2017

Sorry about that. Looks like I did not update the code example correct. Here is the updated version I've been working off of. I believe the syntax for the array is correct and the onSuccess method is still not working. With the onSuccess method removed, the form submits as it should and the data is being pulled into the lead database correct.

Marketo Form Submit - JSFiddle

Would I need to pass in the values of the array in onSuccess? The end goal is to display a success message in the same location on the page as the form used to be instead of directing the user to a new URL/reloading the page.

Thank you for all the help @Sanford Whiteman​ !!

SanfordWhiteman
New Participant
June 9, 2017

It's not the onSuccess, it's that you have this broken code:

You can't declare objects like that -- it's a syntax error.

The fieldMap is an array of objects:

See Custom Form to Marketo Form Submit

June 8, 2017

Ah sorry about that,  @Sanford Whiteman 

Checking for the form ID worked great! The last thing I was hoping to achieve with these custom forms is the ability to control the onSuccess message / URL.

By adding the onSuccess method to the function, I'm preventing the page reload successfully, but the hidden field values are not being submitted. Guessing I'm calling this method too soon?

Marketo Form Submit - JSFiddle

SanfordWhiteman
New Participant
June 6, 2017

It's @Sanford Whiteman actually.

You can always target a single Marketo form by checking the ID:

MktoForms2.whenReady(function(form){

var formId = form.getId();

if ( formId == 999 ) {

   /* stuff for only form 999 */

}

});

June 6, 2017

Hi @Sanford Winfield​! Using this code example, is it possible to have multiple custom forms on the same page, listening for each submit event specifically?

Right now, we are getting duplicate entries if the user is filling out just one form since there are multiple forms present. How would we modify the event listener to be specific to each form only?

Thanks!