You are misreading your code. It's working:
- exactly as expected;
- exactly as you want it to!
When you reference form.test
, it will be a list of all the values you have selected in the checkboxes. EG: if you've checked them all, then the value will be 1,2,3,4
You are demonstrating this to yourself with this bit of your code:
<cfoutput>#i# ---#test#<br /></cfoutput>
You're seeing ---1,2,3,4
. IE: form.test
contains 1,2,3,4
When one loops over a list like this:
<cfloop index="i" list = "#test#" delimiters="," >
Then i
will take each element value of the list in turn. And you are also seeing this.
And the length of form.test
is indeed 7
: "1,2,3,4"
is seven chars. listLen()
would give you 4
.
Finally, some coding suggestions:
- scope your variables:
form.test
nottest
- do not use
isDefined()
, usestructKeyExists()
- I know this is sample code, but
i
is a poor variable name in this context (and probably confusing you),checkBoxValue
might be better. Or something that indicates what the checkbox represents. - if your delimiter is a comma, you do not need to specify it in list operations (functions, loops, etc). Comma is already the default.