1

I have a front end form with multiple checkboxes. Is it possible to convert the checkbox values from an array to a string before it's submitted to the database?

I want to save the checkbox values into a PlainText Craft field in the database.

If I try to submit the front end form checkbox values into a PlainText field currently, I get the error: trim() expects parameter 1 to be string, array given

It looks like perhaps using the join or merge filter may accomplish this but I'm not sure.

2 Answers 2

1

If you have access to jQuery in your project, you can do this.

Change the name of your checkboxes so craft doesn't look at them when the form gets posted and have a hidden input (named inline with your form) that you populate using using js.

This uses jQuery but you can convert to Javascript if needed. It's not perfect but it might get you on the way :)

10
  • Yes, I am using jQuery on my site. Where would I put the actual fieldname of the Craft field 'fields[logGeneral_logInputIndications]' ? I'm assuming I put the jQuery as a hidden form field?
    – Lori
    Commented Feb 1, 2019 at 17:39
  • I'm getting the following as my submitted value: function updateP() { var text = ''; $(
    – Lori
    Commented Feb 1, 2019 at 17:53
  • It's a front end form but only logged in members can see and submit the form. Yes, I'm still working on this.
    – Lori
    Commented Feb 1, 2019 at 18:01
  • I'm using that basic code, but it's not a guest entry submission form.
    – Lori
    Commented Feb 1, 2019 at 18:02
  • For some reason, I still can't get it to work. Here's a the code I'm using: codepen.io/anon/pen/exWOQq
    – Lori
    Commented Feb 1, 2019 at 18:30
1

If you need to do this in a twig template, the join filter will do the job.

Use {{ yourCheckboxesValues|join(', ') }} to print something like that : value1, value2, value3.

8
  • I may be misunderstanding your response @Maxime but how would I capture the joined value and submit that to my database entry? I’m wondering if I need to use a variable to collect the values and then submit the variable to the form field in the database.
    – Lori
    Commented Feb 1, 2019 at 16:43
  • Do you use a plugin or a custom module to submit your form ? Commented Feb 1, 2019 at 17:22
  • No, I am not using any custom module to submit the form. It's just a regular form.
    – Lori
    Commented Feb 1, 2019 at 17:40
  • Are you submitting via ajax? Is so the answer of @Oliver Bon is a more efficient way to achieve your goal. If not; you can also create a custom module and hook a function to transform your field data before its saved on your entry (or sent by mail?) Commented Feb 1, 2019 at 18:16
  • No, I'm not currently submitting using Ajax. I'm just submitting it normally. Sounds like the join option you submitted requires additional module which I don't have the skills to do. But thank you so much for your input. I was just wondering if I was missing something with the join filter.
    – Lori
    Commented Feb 1, 2019 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.