Skip to main content
Derek_Vansant4
Level 2
Dec 14, 2015 8:04 PM UTC
Solved

Auto Submit Marketo Form on Page Load

I have a use case where I want a marketo form to pre-populate fields based on url parameters and then automatically be submitted on page load  without any user clicks. I would have thought the code below would work, but it doesn't. Instead, the code makes the page reload over and over again. If it matters, the page is a marketo landing page on a marketo server.

Anyone ever tackle this use case before? Any ideas why the code below doesn't work?

<body>

<div class="mktoContent">

 

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

<form id="mktoForm_59"></form>

<script>MktoForms2.loadForm("//app-sj11.marketo.com", "558-YIS-558", 59);</script> 

</div>

<script type="text/javascript">

function formAutoSubmit () {

var frm = document.getElementById("mktoForm_59");

frm.submit();

}

window.onload = formAutoSubmit;

</script>

</body>

Kenny_Elkington
Kenny_ElkingtonAccepted solution
Adobe Employee
Dec 14, 2015 10:05 PM UTC

Check out my blog on a similar topic here: http://developers.marketo.com/blog/make-a-marketo-form-submission-in-the-background/​  You would want to use the MktoForms.whenReady method to then call submit() from the form.

16 replies

Derek_Vansant4
Level 2
Dec 14, 2015 9:38 PM UTC

So, testing outside of Marketo, I learn that the code above works if there is no submit button code on the form. Not sure how you remove the button input from a Marketo form or work around it.

Translate
Kenny_Elkington
Adobe Employee
Kenny_ElkingtonAdobe EmployeeAccepted solution
Adobe Employee
Dec 14, 2015 10:05 PM UTC

Check out my blog on a similar topic here: http://developers.marketo.com/blog/make-a-marketo-form-submission-in-the-background/​  You would want to use the MktoForms.whenReady method to then call submit() from the form.

Translate
Derek_Vansant4
Level 2
Dec 14, 2015 10:32 PM UTC

Thank you!

Translate
SanfordWhiteman
User Group Leader
User Group Leader
Dec 14, 2015 10:14 PM UTC

Definitely use Kenny's Forms 2.0 API method.  Don't treat Marketo forms like generic forms.

But also, if the form's Thank You logic reloads the same page, then you will trigger the logic again, which you definitely don't want.  Instead, you should either redirect to another page that doesn't auto-submit (based on query string or a totally different URL) or change the redirect logic to just stay put on the current page.

Translate
Robb_Barrett
Level 10
Dec 15, 2015 12:44 AM UTC

Couldn't this be more easily accomplished in a workflow?  Visits Web Page in the smart list and then some formalized change data value steps in the flow.

Translate
Robb Barrett
SanfordWhiteman
User Group Leader
User Group Leader
Dec 15, 2015 12:55 AM UTC

Not really, because URL parsing isn't going to happen in the CDV step.

Translate
Level 9
Sep 19, 2016 8:35 PM UTC

I have a similar use case, and from the resources above I think I've cracked most of it. But for some reason, the form is submitting multiple times before it redirects (up to 6 times!)

Here is the code. I've put this at the bottom of a Marketo guided landing page template:

<script>

MktoForms2.loadForm("//app-ab06.marketo.com", "110-AIL-152", 1417, function(form) {

    // From here we have access to the form object and can call its methods

  MktoForms2.whenReady(function (form) {

        form.submit();

   });

 

  //Add an onSuccess handler

    form.onSuccess(function(values, followUpUrl) {

        // Take the lead to a different page on successful submit, ignoring the form's configured followUpUrl

        location.href = "google.com";

        // Return false to prevent the submission handler continuing with its own processing

        return false;

       });

 

});

</script>

Maybe I don't need the custom redirect code in the second half - a bit unsure! Any guidance appreciated

Phil

Translate
SanfordWhiteman
User Group Leader
User Group Leader
Sep 19, 2016 8:47 PM UTC

Please post a link to your page. The redirect loop shouldn't happen if you're going to a page built off another template.

You don't need custom onSuccess if you want to use the value from Form Editor.

Translate
Level 9
Sep 20, 2016 1:46 PM UTC

Hi Sanford

Here's the link: http://letsgo.gadventures.com/Agent---National-Geographic-Content-Loading.html

The page it is linking to is built off a new template, yes.

Thanks, Phil

Translate