Being a novice to this topic, I would like to ask how to save the results of a checkbox group as an array, and then afterward save the array to a database when the form is submitted.
Here’s a screenshot image of the form’s checkbox group:
Checkbox1 value = Board, Checkbox2 value = Day, Checkbox3 value = Night. Whatever is checked would be saved as a comma separated value to the School Type column of the database.
Any help is very appreciated. Thank you for your consideration.
Edit: Sorry I didn't read the paragraph after the screenshot, my instructions are not taking this in consideration. I've added a 2nd edit down below after my original reply to cover that case
Original reply below:
Receive 3 variables, for the checkboxes:
$_GET.board
$_GET.day
$_GET.night
In your Server Action steps, place a Group step:
Group checkboxes
Set Value board = $_GET.board
Set Value day = $_GET.day
Set Value night = $_GET.night
Then Database Insert, select the group variable you've created to insert into a column - hopefully that works. If it doesn't work you might need to use the .toJSON() formatter to convert to a JSON string
In the database store as JSON/JSONB datatype, or as string
Edit 2:
Can you use the "join" formatter on the Group variable to join by a comma? The output would be a string
if you name all your checkboxes in the form the same for example check[] (the brackets create the array) this will then when you import to the server action be a POST value with multiple selected.
Then if you use $_POST.check.join(',') on the insert/update it will input a comma seperated array
Thank you all for your help and suggestions. Each had a perspective that ultimately helped me to arrive at a successful end, and I appreciate it very much!
This is what I finally settled on. After playing around with all the differing solutions. I started thinking with logic and finally the following dawned on me:
Actually, I had looked at your post several days earlier.
But the checkboxes you use are hard-coded and don’t rely on dynamic values.
My checkboxes are generated after reading how many options and values exist in the source table.
It saves time and code and the same code will work again when the options change in another form on another project. All I have to do is have the User Admin save their own specific options.
My problem this time is that I have to allow Multiple Options to BE SELECTED, so I need the commas separating each option value selected.
However, maybe the code syntax you are using in the last part I can use.
Thank you. Unfortunately, the value[] solution can’t work with my code because the repeat looping just outputs as many value[] as the options themselves.
There needs to be only one value[] .
If 3 checkboxes are selected the insert sees 3 separate inserts and the only value inserted will be the last one.
Only the dropdown list I use handles a comma-separated string the correct way. Because it is not an array, it is simply one string of text with some commas.
If you want have only one of the series of checkboxes or radio buttons to be selected, then you will have to name all the inputs names the same. The values can be different, but the name must be the same. My example is different in that it is more static than dynamic. I needed a selection that would allow the user to choose to check only one or all selected.
You could take a look at what Teo said about dynamic checkboxes: