Multiple Checkbox's submit to single database field (SOLVED)

Hi All,

I’m slightly confused on how to setup this up…

I need to select up to 5 checkboxes (see below) and submit them to a field called “typeOfInstallation” in the database.


Checkbox ID’S:

  • “communicationBoard”
  • “roomSetting”
  • “stockDisplay”
  • “handBoard”
  • “loosePiece”

I’m aware I need to do the following;

  1. Creating an onClick event for the Submit button.
  2. Creating an array and for each checked checkbox, add its value to the array (creating an array of strings with the relevant values).
  3. Insert the array and the other information added by the user to the collection using the Wix-Data API .

I’m completely lost as to where to start, can anyone give guidance or samples on this to get me on the right track??

Any help would be greatly appreciated.
Thanks

1 Like

Hay Conor,

Depending on how you work, you have two ways to solve this issue - using a dataset for the form, or using plain code.

If using a dataset, you have the beforeSave dataset hook that you can use to update the saved item using code - with the setFieldValue function of the dataset…

It will look something like the following -

$w.onReady(() => {
  $w('#DATASET1').onBeforeSave(() => {
    let checked = [];
    if ($w('#CHECKBOX1').checked)
     checked.push('value1');
    if ($w('#CHECKBOX2').checked)
     checked.push('value2');
    if ($w('#CHECKBOX3').checked)
     checked.push('value3');
    $w('#DATASET1').setFieldValue('checkboxes', checked);  
  })
})

If using the wix data api directly, you can just create the array and insert the item with an array field to the database.

Hi Yoav,

Thanks a lot for your tip - I got it working!!!
Now to set this up with over a hundred checkboxes lol… :slight_smile:

Cheers!

question about the beforeSave. Can I use to set up a prefill for form entry?

Have a form for authors to enter an article, but want their photo, bio, etc to prefill to dataset so they do not have to enter each time. They just enter the article and its info, and use a beforeSave with submit button to pull the bio info into a reference field in dataset. Know the logistics of what I want, but no idea of a code set up to make work.

Hi Yoav or Conor

Could you please show me an example of the code for the part where you said:
“If using the wix data api directly, you can just create the array and insert the item with an array field to the database.”

My form has a couple of sections/questions that have multiple options to select from. So I would like to know the best way to get the multiple selections(checkboxes) into the wix database under one field. (to show that this one applicant selected all these options for this particular question.

Looking forward to any assistance.
Thanks!

Hi Jpokelo9,

Below is an example of how I utilised multiple checkboxes and push them to one single cell in the database;

$w.onReady(() => {
  $w('#dataset1').onBeforeSave(() => {
    let checked = [];
    if ($w('#CommunicationBoard').checked)
     checked.push(' Communication Board');
    if ($w('#RoomSetting').checked)
     checked.push(' Room Setting');
    if ($w('#StockDisplay').checked)
     checked.push(' Stock Display');
    if ($w('#HandBoard').checked)
     checked.push(' Hand Board');
    if ($w('#LoosePiece').checked)
     checked.push(' Loose Piece');  
    $w('#dataset1').setFieldValue('typeOfInstallation', checked);  
  });
});

#dataset1 = change this for your dataset name
#CommunicationBoard, #RoomSetting, #StockDisplay, #HandBoard, #LoosePiece = your assigned value of your checkboxes.
#TypeOfInstallation = change to whatever column you want the checkbox to go in your database.

I hope this helps!

Kind Regards,
Conor

Thank you so much Conor!

This has really helped me get closer to getting it to work.
However, it seems like my Database is not recognizing the Field name, and is trying to create a new field with the same name except it is enclosed in [ ] . *See below . Any idea how I can fix it? .

When I try to " Add field to schema " , I get:


After I Change to Text , the input is converted to Text format …which is great, but I am still left with two Fields with instead of one.


…this is the code I used:

$w.onReady(() => {
  $w('#dataset1').onBeforeSave(() => {
    let checked = [];

    if ($w('#coreStrength').checked)
     checked.push(' Improve Core Strength');
     
    if ($w('#lowerBody').checked)
     checked.push(' Improve Lower Body Strength'); 
     
    $w('#dataset1').setFieldValue('Fitness Development Goals', checked);
  });
});

Thank you so much for you help so far!
Paul

Hi Paul,

Glad this has set you on the right path!

I think your code is wrong;
You have:

" $w(’ #dataset1 ').setFieldValue(‘Fitness Development Goals’, checked); }); }); "
In a database cell ID’s cannot have spaces and I assume this is why it’s duplicating; therefore double check this, but I would imagine it would be something like the following;

 $w('#dataset1').setFieldValue('FitnessDevelopmentGoals', checked); }); });

OR

 $w('#dataset1').setFieldValue('Fitness_Development_Goals', checked); }); });

This should work!

Kind Regards,
Conor

Hi guys. I’m experiencing the same issue but the field’s names where I use setFieldValue (all of them) are replicate in the collection even they had been created without any space in the names. The new fields got the values passed to them and the original fields stay blank.

This is so helpful! I have a question, how do you then take the same check box data and then populate it in a dynamic page? For example, if I have check box tags, I would like the selected check box tags to then display back on the dynamic page.

@conor-keogh verifying…no properties need to be set for the checkboxes such as onclicks or ??

When I test on Preview/Publish , when I click on one checkbox all the other boxes get checked too? Can you please help.

so … i have a checkboxgroup with 3 options (like how dropdowns worsk).
i also have a column in my dataset with the type: tags
i want these to fit exactly my item preferences ( e.g. if i check music and food to show me an item with exactly both characteristics and no with at least 1 characteristic)
can u help me?

@stevenevenavendoven This post will help you to find your solution…
https://www.media-junkie.com/pflegeservice