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.
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.
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.
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.
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);
});
});
" $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;
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.
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?