Forum

Thread tagged as: Question, Forms

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/

Creative Monster

Creative Monster 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Rachel Andrew

Rachel Andrew 394 points
Perch Support

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:

<perch:input type="checkbox" id="test[]" label="Test 1" />
<perch:input type="checkbox" id="test[]" label="Test 2" />

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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, like that, but also set values.

<perch:input type="checkbox" id="test[]" label="Test 1" value="test1" />
<perch:input type="checkbox" id="test[]" label="Test 2" value="test2" />

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:

Warning: stripslashes() expects parameter 1 to be string, array given in /Users/Toby/Sites/create-this/ace/core/lib/PerchUtil.class.php on line 1165

Within Perch, the Forms app registers a form submission but there is no detail, just the date that the form was submitted.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, certainly. Something like:

$API = new PerchAPI(1.0, 'my_app_name');

$Email = $API->get('Email');

// Set which HTML template to use
$Email->set_template('my_app/contact.html');

// Set the variables for the template
$Email->set('greeting', 'Welcome!');
$Email->set('foo', 'bar');

// Who to send it to
$Email->recipientEmail('foo@example.com');

// Who to send it from
$Email->senderName(PERCH_EMAIL_FROM_NAME);
$Email->senderEmail(PERCH_EMAIL_FROM);

// Send it!
$Email->send();

Great, I'll give this a go.