Auto Submit Marketo Form on Page Load | Community
Skip to main content
Derek_Vansant4
New Participant
December 14, 2015
Solved

Auto Submit Marketo Form on Page Load

  • December 14, 2015
  • 4 replies
  • 7927 views

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>

Best answer by Kenny_Elkington

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.

4 replies

New Participant
September 19, 2016

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

SanfordWhiteman
New Participant
September 19, 2016

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.

New Participant
September 20, 2016

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

Robb_Barrett
New Participant
December 15, 2015

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.

Robb Barrett
SanfordWhiteman
New Participant
December 15, 2015

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

SanfordWhiteman
New Participant
December 14, 2015

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.

Derek_Vansant4
New Participant
December 14, 2015

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.

Kenny_Elkington
Kenny_ElkingtonAccepted solution
New Participant
December 14, 2015

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.

Derek_Vansant4
New Participant
December 14, 2015

Thank you!