SOLVED

Re: Prevent Lead/Contact Creation on Landing Page Form Submission?

Go to solution
Gareth_Bromser
Level 1

Prevent Lead/Contact Creation on Landing Page Form Submission?

Hiya,

Within a Marketo landing page, that uses a form, I am using the form API's onSubmit() method to make an external POST call to a remote endpoint when the form is submitted.  Works well.

When the form is submitted this of course also triggers creation of a lead in the Marketo database.  Is there any way to prevent this from happening?  So that the form can still be submitted, run the script tied to onSubmit(), but not actually create a new lead in Marketo.

The code I am adding to the LP (with a <script> tag) is below.  Anything I can add here that would achieve the above and prevent Marketo lead creation?  I thought maybe returning immediately after the API call might do it, but no luck.  Thank you!!

async function sendFormData() {
   MktoForms2.whenReady(function(form) {
      form.onSubmit(async function() {
         const vals = form.vals();

         const valsToSend = {
            ...vals,
            Source: 'marketoLp',
         };

         const valsToSendJson = JSON.stringify(valsToSend);

         await fetch(REMOTE_API_URL, {
            method: 'POST',
            headers: {
               'Content-Type': 'application/json',
            },
            body: valsToSendJson,
         });

      return null;
      });
   });
}

sendFormData();
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Prevent Lead/Contact Creation on Landing Page Form Submission?

The Nation has a Syntax Highlighter for JS. Use Advanced Editor and expand the toolbar.

pastedImage_1.png

What you want to do is

form.submittable(false);

Then you can do whatever you want after your separate XHR POST.

Also, are you purposely restricting your LP so it only works on browsers that support async/await and destructuring?

View solution in original post

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Prevent Lead/Contact Creation on Landing Page Form Submission?

The Nation has a Syntax Highlighter for JS. Use Advanced Editor and expand the toolbar.

pastedImage_1.png

What you want to do is

form.submittable(false);

Then you can do whatever you want after your separate XHR POST.

Also, are you purposely restricting your LP so it only works on browsers that support async/await and destructuring?

Gareth_Bromser
Level 1

Re: Prevent Lead/Contact Creation on Landing Page Form Submission?

Thank you Sanford!  Will be using the syntax highlighter from now on.

This does exactly what I wanted, perfect solution.

Great question, I'm gonna just use a promise instead of async/await and remove spread syntax when I make it live.  Not sure if it'll need IE support yet but will add polyfills if so

SanfordWhiteman
Level 10 - Community Moderator

Re: Prevent Lead/Contact Creation on Landing Page Form Submission?

I always advise IE compatibility. IE 11 is out there. Unless you have stats that show it never hits your site.