2 Replies Latest reply on Sep 13, 2017 12:14 PM by Matt Fiel

    Best way to submit user entered javascript variables via Forms 2.0?

    Matt Fiel

      Hello community,

       

      I have a question to see if I'm approaching my project the best way I can.

       

      We're creating a 10 question quiz.  The quiz is designed in a way that wouldn't work well with a standard Marketo form (the quiz is a series of connected screens, one question displayed at a time, all on one page), although we'd ultimately like to have user answers submitted to our lead database.

       

      To achieve this, it seems that the “.addHiddenFields(values)" method using forms 2.0 would work well.

       

      On the page, users would click buttons (styled div containers) to indicate their answer to a question.  These button clicks would initiate jQuery events that assign each answer to a variable – in this case, the array “answer[]”.

       

      The email address field would be an HTML input field. Ultimately, I submit like this:

       

      $(".complete").click(function(){

       

           MktoForms2.whenReady(function(form){   

                form.addHiddenFields({

                     "Email": email,

                     "q1": answer[1],

                     "q2": answer[2],

                    "q3": answer[3],

                    "q4": answer[4],

                    "q5": answer[5],

                    "q6": answer[6]

           });

         

           form.submit();

                form.onSuccess(function(callback) {

                return false;

           });

       

      });

       

      });

       

       

      The code works – but is this a sound solution?  From what I’ve read, the form.submit() method performs all validation, so it’s good from that perspective - but I wanted to make sure there wasn't a better way.