Passing value to empty form isn't working. | Community
Skip to main content
Ankit_Patel1
Level 2
June 18, 2019
Question

Passing value to empty form isn't working.

  • June 18, 2019
  • 2 replies
  • 3893 views

Passing value to empty form (form submission in the background) isn't working. Any help with the code?

(https://developers.marketo.com/blog/make-a-marketo-form-submission-in-the-background/)


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://app-lon08.marketo.com/js/forms2/js/forms2.min.js"></script>
<form id="mktoForm_10XX" style="display:none"></form>

<script>
MktoForms2.loadForm("https://app-lon08.marketo.com", "162-XXX-XXX", 10XX); 
MktoForms2.whenReady(function(mktoForm)

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

     myForm.addHiddenFields({

     "Email": $("#mwf3723fced2f65").val() ,
     "FirstName": $("#mwfca1d8fb14840").val(), 
     "LastName": $("#mwf2e0f8bf4c5a5").val(), 

  });
mktoForm.submit();
e.preventDefault();

});

</script>

Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

SanfordWhiteman
Level 10
June 18, 2019

Please highlight your code using the Advanced Editor's syntax highlighter so it's readable.

And provide as much detail as possible, not just "not working" but a description of your errors/logs and a link to your page.

Jay_Jiang
Level 10
June 19, 2019

If you're using the code exactly as is, the code is submitting the Marketo form as soon as it's loaded and ready, and most likely when your 'pretty' form is still empty

You want to wrap the Marketo form submission in a click or submit event that triggers the form submission

e.g.

MktoForms2.loadForm("https://app-lon08.marketo.com", "162-XXX-XXX", 10XX); 
MktoForms2.whenReady(function(mktoForm) {
var myForm = MktoForms2.allForms()[0];
$('#prettyForm').submit(function(e){
myForm.addHiddenFields({
"Email": $("#mwf3723fced2f65").val() ,
"FirstName": $("#mwfca1d8fb14840").val(),
"LastName": $("#mwf2e0f8bf4c5a5").val()
});
mktoForm.submit();
});
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

be mindful if your pretty form is also actually submitting, because it may navigate away from the page before the marketo submission is successful

SanfordWhiteman
Level 10
June 19, 2019

Also, e doesn't exist in any of the scopes shown, so e.preventDefault() will throw a fatal error.

(And the original code must still be highlighted, it's not realistically readable for this kind of thing.)