Forms 2.0 API — AddHiddenFields() and Checkboxes

jtremblay
Level 1

Forms 2.0 API — AddHiddenFields() and Checkboxes

I'm working on our implementation of background form submission as described in this Marketo blog post, and suggested to us by their support team.

In doing so, we've run into a challenge (which was noted as "not so easy" in this previous forum answer).  We need to submit a checkbox group through a background form submission.

I've tried adding multiple checked values one at a time using `addHiddenFields()`...

 

myMktoForm.addHiddenFields( 'My_Field_Name', 'Checked Value 1' );
myMktoForm.addHiddenFields( 'My_Field_Name', 'Checked Value 2' );
myMktoForm.addHiddenFields( 'My_Field_Name', 'Checked Value 3' );

 

But when I call `getValues()` I only see the last one...

 

{
  My_Field_Name: 'Checked Value 3'
}

 

I've also tried adding them as an array...

 

myMktoForm.addHiddenFields( 'My_Field_Name', [
  'Checked Value 1',
  'Checked Value 2',
  'Checked Value 3'
);

 

In that case the values in the array get merged into a comma-separated string...

 

{
  My_Field_Name: 'Checked Value 1,Checked Value 2,Checked Value 3'
}

 

So next, I tested a checkbox group on a Marketo landing page to see how the values were handled. If I add a checkbox group on a Marketo landing page, check some values, then call `getValues()`, the values appear to be kept as an array, not merged into a string...

 

{
  My_Field_Name: [
    'Checked Value 1',
    'Checked Value 2',
    'Checked Value 3'
  ]
}

 

So I'm a bit stuck at how to reproduce the expected behavior before submitting the background form... is there a clever way to add a hidden field containing an array of values using the Forms2 API?

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Forms 2.0 API — AddHiddenFields() and Checkboxes

If a field isn’t on the form, then it isn’t actually a Checkboxes group — <input type="hidden"> is equivalent to <input type="text" hidden>.

 

let serializedMultiString = ['Checked Value 1','Checked Value 2','Checked Value 3'].join("; ");
myMktoForm.addHiddenFields({
  My_Field_Name : serializedMultiString
});
jtremblay
Level 1

Re: Forms 2.0 API — AddHiddenFields() and Checkboxes

Thanks for the tip @SanfordWhiteman, I'll try that approach and report back.