Help with Checkboxes, Select All that Apply

Hi, I’m trying to build a customer form and use checkboxes to give customers the ability to select all of the options that apply. I know how to use radio buttons to get one answer or text input to get a string.

Is there a way I can take the values from the checked checkboxes and put them together in one text string then send that value to the database? Or is there a more efficient way to do this?

Also, is there a way to make it so that when customers check a checkbox and hit submit, they’re automatically subscribed to the mailing list, or do I need to enter their email manually?

Thank you!

1 Like

I would recommend watching this highly easy to follow tutorial on How to Create a Custom Form & Connect It to a Database .

Also, take a look at the Basic Form example . You can load the entire project into your editor and play around with it. This will let you see how things work, you can make changes, and perhaps even adapt it for your own use.

Good luck,

Yisrael

Yisrael,

Thank you for your response. I’ve watched the video and looked at a couple examples. Unless I’m missing something. It still doesn’t explain how to use the checkboxes to select more than one option from a list.

An example of what I’m trying to do is say, “Which types of accommodations are you comfortable with ? Select all that apply” then give the options “Plane”, “Trains”, and “Automobiles”. If they only want 2 of the 3 options, there’s not an easy way to document that other than making each option a TRUE or FALSE Boolean object in the database, but I have a lot of options.

I know I can set a value to each checkbox then assign the value of that ceckbox to the database using Wix API commands, but is there a way to combine the selected values into a string? Do you recommend a different approach?

Hi,

I think that it depends on the use case, but I’m not sure if string concatenation is what you need.
I would suggest adding a field to each checkbox in the database. It will be much easier to use after.

Anyway, if you do want to concat strings of all checkboxes in the page:


let allValues = '';
$w('Checkbox').forEach(checkbox => {
    allValues += checkbox.value;
});
console.log(allValues);

Liran.

How would you write the code if you have multiple questions and have multiple check box options.
Eg.

FORM VIEW

  1. Question 1. Select all that apply ?
    **Checkbox 1 ** Checbox 2 ** Checbox 3
  2. Question 2 Select all that apply ?
    **Checkbox 1 ** Checbox 2 ** Checbox 3
  3. Question 3 Select all that apply ?
    **Checkbox 1 ** Checbox 2 ** Checbox 3

DATABASE VIEW would display the following for each response. eg:

Field Column Title : Question 1
Response : Checkbox 1, Checkbox 2,

Field Column Title : Question 2
Response : Checkbox 2, Checkbox 3

Field Column Title : Question 3
Response : Checkbox 1, Checkbox 2, Checkbox3

I am trying to avoid having each Checbox item as a separate field /column in the database.
I would like all the selections for eg Question 1 to appear in one field separated by commas.

Hi,

There are number of ways to achieve that.
For example you could name each checkbox as such: q1checkbox1, q1checkbox2 etc, q2checkbox1 etc. and based on Liran’s code add the following if statements:

let firstQValues = '';
let secondQValues = '';
let thirdQValues = '';

$w('Checkbox').forEach(checkbox => {
 if (checkbox.id.startsWith('q1')) {
    firstQValues += checkbox.checked;
  }
 if (checkbox.id.startsWith('q2')) {
    secondQValues += checkbox.checked;
  }
 if (checkbox.id.startsWith('q3')) {
    thirdQValues += checkbox.checked;
  }
});