Javascript help: passing through values on form success | Community
Skip to main content
August 25, 2016
Solved

Javascript help: passing through values on form success

  • August 25, 2016
  • 1 reply
  • 3516 views

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!

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

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.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
August 25, 2016

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.

August 25, 2016

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