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

Anonymous
Not applicable

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

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.

Tags (2)
2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

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

Yes and no. Adding to the whenReady stack unnecessarily would be a real problem if people can click "Complete" multiple times and not pass validation.  I would reverse it, and also make the object creation less repetitive:

MktoForms2.whenReady(function(form){  

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

      var mktoFields = {};

     

      mktoFields["Email"] = email;

      answer.forEach(function(a,q){

         mktoFields["q" + q] = a;

      })

      form.addHiddenFields(mktoFields);

      form.submit()

   });

   form.onSuccess(function(callback) {

      return false;

   });

});

Anonymous
Not applicable

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

Thank you!