SOLVED

Javascript help: passing through values on form success

Go to solution
Anonymous
Not applicable

Javascript help: passing through values on form success

Hey guys,

I'm wondering if anyone who knows Javascript (because I know pretty much nothing about Javascript) can help me. I have a landing page that is being used for Google Ads and have been asked what values gets passed through upon form submission.

I've been given this string as an example:
on_sent_ok: "ga('send', 'event', 'Contact Form', 'submit');"

But I'm not sure how I would even implement this. However, I currently have this block of JS code that executes when the form is submitted:

<script>

MktoForms2.whenReady(function (form) {

    // Add an onSuccess handler

    form.onSuccess(function(values, followUpUrl) {

        // Get the form's jQuery element and hide it

        form.getFormElem().hide();

        // Grab HTML element and display Thank You message in element

        document.getElementById("thankyou").innerHTML = "<h3>{{my.CTA Follow Up Copy:default=Thanks for subscribing!}}</h3>";

        // Return false to prevent the submission handler from taking the lead to the follow up url

        return false;

    });

});

</script>

Is there some way that I could insert that string into this block of code? Again, keep in mind I don't know much about JS. I can only read through it and sort of decipher.

Any help would be appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Javascript help: passing through values on form success

But I'm not sure how I would even implement this. However, I currently have this block of JS code that executes when the form is submitted:

Technically, that code executes after the form has been submitted, not when (onSuccess is critically different from onSubmit).

In your specific case, you can add the line

     ga('send', 'event', 'Contact Form', 'submit');

directly above the line

     return false;

in your onSuccess function.

However, this only works because you are staying on the same page after form submit. If you were instead redirecting the user to another page, you could not do the GA logging this simply, and otherwise comparable code that attempts to do so is broken.

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Javascript help: passing through values on form success

But I'm not sure how I would even implement this. However, I currently have this block of JS code that executes when the form is submitted:

Technically, that code executes after the form has been submitted, not when (onSuccess is critically different from onSubmit).

In your specific case, you can add the line

     ga('send', 'event', 'Contact Form', 'submit');

directly above the line

     return false;

in your onSuccess function.

However, this only works because you are staying on the same page after form submit. If you were instead redirecting the user to another page, you could not do the GA logging this simply, and otherwise comparable code that attempts to do so is broken.

Anonymous
Not applicable

Re: Javascript help: passing through values on form success

Sanford Whiteman​ That was most helpful. Thanks! Didn't even realize there was a difference.