SOLVED

Stop hidden auto-submit form from submitting data if email field is empty

Go to solution
Highlighted
Level 1

Stop hidden auto-submit form from submitting data if email field is empty

I currently have a hidden form which auto-submits upon page load using the following code:

 

<script src="//app-abXX.marketo.com/js/forms2/js/forms2.min.js"></script>
<form id="mktoForm_3023" style="display:none;"></form>


<script>

MktoForms2.loadForm("//app-abXX.marketo.com", "XX-XX-XX", 3023, function(form) {
    
    // Add an onSuccess handler
    form.onSuccess(function(values, followUpUrl) {
        // Get the form's jQuery element and hide it
        form.getFormElem().hide();
        // Return false to prevent the submission handler from taking the lead to the follow up url
        return false;
    
    });
    
    form.submit();

});

</script>


We use this form in conjunction with URL parameters to update fields on the fly for our website visitors based on their actions. Recently, we've started to notice that on one of our landing pages, due to this auto-submitting form, new records are being created for users that are already in our database causing a bit of a headache for our reporting since records are being duplicated.

 

Is there any way to stop the form above from auto-submitting if the email field is empty? 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Level 10 - Community Moderator

Re: Stop hidden auto-submit form from submitting data if email field is empty

MktoForms2.loadForm("//app-abXX.marketo.com", "XX-XX-XX", 3023);
MktoForms2.whenReady(function(form){
  let currentValues = form.getValues();

  form.onSuccess(function(values, followUpUrl) {
    form.getFormElem().hide();
    return false;    
  });
    
  if(currentValues.Email){
    form.submit();
  }
});

View solution in original post

3 REPLIES 3
Highlighted
Level 10 - Community Moderator

Re: Stop hidden auto-submit form from submitting data if email field is empty

Of course you can do it using JS, but the easier way is to just make the Email field required. Marketo will not be able to submit the form if the Email field is req'd, even/especially when it's hidden.

Highlighted
Level 1

Re: Stop hidden auto-submit form from submitting data if email field is empty

Thanks for the speedy reply.

 

Right now, I have the email field set as a hidden field pulling in values via URL parameter. I don't believe I'd be able to do that if I set the field as an email field and as required. I don't believe I get the option to pre-fill the field with a URL parameter at that point.


Note: this form is embedded in a non-Marketo landing page.

Highlighted
Level 10 - Community Moderator

Re: Stop hidden auto-submit form from submitting data if email field is empty

MktoForms2.loadForm("//app-abXX.marketo.com", "XX-XX-XX", 3023);
MktoForms2.whenReady(function(form){
  let currentValues = form.getValues();

  form.onSuccess(function(values, followUpUrl) {
    form.getFormElem().hide();
    return false;    
  });
    
  if(currentValues.Email){
    form.submit();
  }
});

View solution in original post