DMXzone App Connect Support Product Page

Answered

How do I insert multiple records from dynamically generated collection of checkboxes?

Asked 03 Mar 2020 14:18:20
1
has this question
03 Mar 2020 14:18:20 Bruce Wilkie posted:
Hi there

I have a form that includes a group of checkboxes that are dynamically generated from a database table.

The form should insert a product into one database table and also a number of optional features for that product into a features table (only those features that have been ticked by the user while filling out the form).

The list of available features is contained in a third table which I've used to create the group of checkboxes within the form. The available features are editable by the user on a separate page, so i can't hard-code the checkboxes into the product insert page like the other product fields.

Below is the page code for the checkboxes:

<div dmx-repeat:repeat1="serverconnectListFeatures.data.queryListFeatures">
<label>
<p><img src="" alt="" class="img-responsive" dmx-bind:src="../images/products/{{feature_icon}}"/></p>
<p>
<input type="checkbox" name="product_features" value="{{feature_id}}" id="product_feature_{{feature_id}}">
{{feature_name}}
</p>
</label>
</div>

How do I set up the Server Action so that it cycles through all the features and, if the box is checked, it inserts a record into the 'feature_link' database?

I thought that i could maybe create a Database Query within the server action that selects all of the features and then add a repeat that uses that query as the source, and then within the repeat I could add a Database Insert step. But when i tried this I couldn't find a way to dynamically pass the id of the checkbox on the page.

prntscr.com/raxm06

Feels like I'm really close, but could be going in totally the wrong direction.

Could you let me know how to do this?

thanks
Bruce

Replies

Replied 05 Mar 2020 14:52:16
05 Mar 2020 14:52:16 Bruce Wilkie replied:
Hi again.

Client's chasing me about this so going to drop an email to tech support in case they've missed this post.

Will post up any response here in case anyone else is looking for this in future
Replied 05 Mar 2020 16:09:26
05 Mar 2020 16:09:26 Teodor Kuduschiev replied:
How do you need these values from your checkboxes stored?
- In a single input, comma separated?
or
- Each value in a new row in your database?
Replied 06 Mar 2020 09:02:53
06 Mar 2020 09:02:53 Bruce Wilkie replied:
Hi Teodor

I'm wanting to store each value in a new row of the database table
Replied 06 Mar 2020 14:27:51
06 Mar 2020 14:27:51 Teodor Kuduschiev replied:
Your checkboxes must have the same name which ends with []
Example: name=“my_checkbox[]”

Then import the form under globals. Then use a repeater step and select > {{$_POST.my_checkbox}} as an expression
Inside the repeat add the insert step, and use {{$value}} for the insert value.
Replied 06 Mar 2020 16:05:11
06 Mar 2020 16:05:11 Bruce Wilkie replied:
thanks Teodor

Tried setting it up as you suggest but I'm getting a 500 internal server error in console window when i submit the form.

I've taken a few screenshots of settings in dreamweaver below:

html for checkboxes and POST variables imported from form
prntscr.com/rcmj8b

reperater settings
prntscr.com/rcmkmg

database insert settings
prntscr.com/rcmlbl

I've obviously made a mistake somewhere interpretting your instructions. If you could have a look and let me know where I'd be grateful.

thanks
Bruce
Replied 08 Mar 2020 09:28:56
08 Mar 2020 09:28:56 Teodor Kuduschiev replied:
Well, please check the exact error message as explained here: www.dmxzone.com/go/32354/debugging-problems-in-dmxzone-server-connect/
Replied 10 Mar 2020 10:40:45
10 Mar 2020 10:40:45 Bruce Wilkie replied:
Hi Teodor

Thanks for that. I figured out the problem. Firstly when I allowed null values in the database table I found that the feature_id wasn't being sent from the form to be inserted into the table, hence the form submission not succeeding due to the insert failing (null values not permitted for the field_id field). Then I found this was due to my error of assigning the feature_id to the checkbox's 'value' instead of assigning it dynamically binding it to the checkbox using the app connect panel (dmx-bind:value="feature_id".

Now I've corrected that and following your instructions on how to set the loop up it's all working great now.

So if anyone comes after me wanting to know how to do this, the screenshot links I put up in my previous post above are all valid, the only difference is the code for the checkboxes that I included in the original post right at the top. The correct, working code is as follows:

<div dmx-repeat:repeat1="serverconnectListFeatures.data.queryListFeatures">
<label>
<input name="product_features[]" type="checkbox" id="product_features" dmx-bind:value="feature_id">
{{feature_name}}
</label>
</div>

Thanks again for your help with this Teodor
Love the T-shirt

Reply to this topic