Coldfusion array from form checkbox

244 views Asked by At

I am trying to figure out the best way to handle this. I have a series of form fields with checkboxes for people to select options. When that gets submitted it turns form.optiongroups into an array. I then check to see if the id of the optiongroup is in the array and set the checked value to true in case there were form errors I want them to retain their checked value. This all works fine.

If I only select one option though it doesn't come through as an array, but just a regular form field. Is there a way I can handle this to make sure it is always an array?

2

There are 2 answers

1
Dan Bracuk On BEST ANSWER

Actually, checkboxes get submitted as a list. You must have something else going on that creates the array.

However, to answer your question as asked, you can use ListToArray(). It would be something like this:

if (structkeyexists(form, 'optiongroups') { // if no boxes are checked the variable will be undefined.
    
    if (isArray(form.optiongroups) == false )
        form.optiongroups = ListToArray(form.optiongroups) 
    } else {  
        code for no boxes checked
    }
2
James A Mohler On

It could also be done via...

param form.optiongroups = "";

form.optiongroups = ListToArray(form.optiongroups);