Submit array of dynamic checkboxes to my SC API script

I’m hoping someone can help me with this.

I have a dynamic form where the questions are pulled in from a query. Some of the questions have multiple checkboxes and the options are stored in a JSON field. I’ve been able to show these checkboxes in the form but I’m struggling with names and ids for submitting to my API script which processes it all. I want to store the checked values in a JSON field in my answers table.

I hope I’ve explained this clearly enough. I think I’m just getting in a muddle with arrays, names, multiples, ids, etc.

Maybe check:

Thanks Teodor. I’ve already gone through that. The bit I’m struggling with is the naming of the fields so they send correctly to my API script. The checkboxes are created using a repeat.

Here’s my html at the moment:

<div dmx-show="questionType=='Checkboxes'">
    <div dmx-repeat:repeat1="questionOptions">
        <div class="form-check">
            <input class="form-check-input" type="checkbox" dmx-bind:value="$value" dmx-bind:id="'checkboxes_'+$index" dmx-bind:name="'checkboxes[]'">
            <label class="form-check-label" dmx-bind:for="'checkboxes_'+$index">{{$value}}</label>
        </div>
    </div>
</div>

You don’t need a dynamic name for these, when using a static value … you need just name="checkboxes[]"

2 Likes

Thanks @Teodor. I’m afraid it’s a little more complicated than that.

My form has nested repeats - one main repeat for each question and then a nested repeat for the checkboxes within the question. I’ve got the form to set the name of the checkbox to checkboxes_1[] where 1 is the parent repeat index but I just can’t work with this in the API. If I hardcode {{$_POST.checkboxes_1}} in the json field it stores it perfectly. But I can’t make the 1 dynamic.

I’ve tried…

{{$_POST.checkboxes_$index}}
or
{{$_POST.checkboxes_+$index}}

How can I achieve this? It’s driving me mad as I’m sure it must be an easy problem to solve but I’m just going round and round in circles.

This might help explain…

It’s part of the INSERT action and shows how the other fields are handled.

{{ $_POST["checkboxes_" + $index] }}

Untested

2 Likes

YESSSSSS!!! That was the syntax I was looking for. You absolute diamond @Apple. Huge thanks, it’s working perfectly.

1 Like