Forum
Create Dynamic Number of Checkboxes using Form App based on content
A client would like a form that lists all products from across the site with a checkbox associated with each one – the user can then check the ones they would like a product sample for and then submit.
Is this achievable with Perch Forms?
It's just standard Perch at the moment with products across different pages, however upgrading to Runway wouldn't be a problem if this will help, as the products would be better suited to a collection now.
The example they gave me is this: https://www.whitneywoods.co.uk/prices-and-samples/
Is the purpose to send these in a contact form?
I think it may be possible to do, although without testing I'm not sure what the output would look like. If you name the field ending with
[]
, the results will be presented as an array of values rather than just a single value. I'm not sure how the Forms app will deal with that, but it should be easy to test.Yes to sending as a contact form.
Could you expand on naming the field to end with [ ], I'm not sure I follow?
What does your checkbox field look like in the template currently?
It doesn't, this is all hypothetical – I didn't want to start work on the template unless I knew it was possible.
I thought about maybe using a layout tag to pull in a layout file that could dynamically create the checkboxes, but that doesn't seem to work.
I would recommend a quick prototype of the functionality to try it out.
Of course I'm happy to try that, but at this stage a prototype would just be a checkbox and and submit button, as the issue is dynamically generating the checkboxes.
Or a number of checkboxes as they'd appear with multiple products.
I could try and do it for you, but you'd need to wait a while before I get a chance.
Yes that's right, but there's no point in hard coding the checkboxes in a template if I can't then generate them on the fly.
Generating them is the easy part. I don't know how the Forms app will process them.
Ah okay, that's the part I'm drawing a blank on. If the checkboxes can be generated with a unique id, wouldn't the Forms app just treat them like any other inputs?
To test this assumption create a form with more than one checkbox and use the syntax that will pass them in as an array, as Drew has suggested.
You can then see if it works or not. If it doesn't work then you can consider how else you might approach the task.
The syntax hasn't been made clear though.
Would it be like this:
However, I'm not sure why they need to be passed as in an array? Why can't they be passed in like any other checkboxes?
Yes, like that, but also set values.
The IDs can't be dynamic, so this is a way around that.
Right, so I've tested this and upon submission the following error is output:
Within Perch, the Forms app registers a form submission but there is no detail, just the date that the form was submitted.
Ok, it looks like it might not be possible in that case. At least not with the Forms app.
Okay, cheers Drew. I guess I can just do it with a standard form and email the selections. Is it possible to send email with the API?
Yes, certainly. Something like:
Great, I'll give this a go.