Reputation: 493
I am trying to post the following :
<input name="answer[1][]" value="Answer 1 question 1"/>
<input name="answer[1][]" value="Answer 2 question 1"/>
<input name="answer[2][]" value="Answer 1 question 2"/>
<input name="answer[2][]" value="Answer 2 question 2"/>
Unfortunately I receive a coldfusion error when posting:
Expression-Element ANSWER is undefined in a Java object of type class [Ljava.lang.String;.
Anyone has an idea on how to solve this problem?
Upvotes: 1
Views: 1698
Reputation: 493
Thanks Adam and Ben for your answers, they have been very helpful and drove me to the solution of the puzzle. I have been coding often in php where using name="xxx[]" creates an array in the $_POST variable. Now Coldfusion throws an error because there is no need of adding the "[]".
What i did is:
<input name="answer_1" value="Answer 1 question 1"/>
<input name="answer_1" value="Answer 2 question 1"/>
<input name="answer_2" value="Answer 1 question 2"/>
<input name="answer_2" value="Answer 2 question 2"/>
Coldfusion will return 2 structures "answer_1" and "answer_2" containing 2 comma separated strings.
But if my string would contain commas eg: Answer 1, question 1
This would give me problems when I will try to transform the string in a list.
Using the method getPageContext().getRequest().getParameterMap() Codfusion transforms the comma separated strings in arrays:
Struct answer_1 would contain an array:
array[1]="Answer 1 question 1" array[2]="Answer 1 question 2"
And this is exactly what i needed !
Upvotes: 1
Reputation: 29870
You can't just call a form field "answers[1][]" and hope that some how ColdFusion will work out that string - because that's all "answers[1][]" is: a string - is supposed to be interpreted as an array reference. CF doesn't work like that.
Even if it did work like that, you'd need to put a value in the second dimension of the array notation: answers[1][]
is not valid CFML.
The first troubleshooting step when variables are appearing to be different to what you expect is to dump the variable out (or in this case, the form
scope out):
<cfdump var="#form#">
This would quickly reveal that you don't have an array, you have a two variables in the form scope: one called answer[1][]
and the other answer[2][]
(where each variable is literally called that). Dot-notation rules in CF won't allow this to be referenced as form.answer[1][]
, you need to reference it as form["answer[1][]"]
.
As of ColdFusion 10, one can have form variables with the same name being set in the form scope as an array, by setting this in your Application.cfc:
this.sameformfieldsasarray = true
However that obviously (?) only works for a single dimension array.
If you want a multi-dimension array, you'll have to DIY with a loop over the form scope and a variable-name check to see which array / dimension each value should go in.
Upvotes: 4
Reputation: 2073
What's probably happening is that ColdFusion is trying to create the FORM Scope using the key "answer[1][]" and evaluating it as an expression which is invalid variable name for ColdFusion/Java. Depending on what you want to do, you'll probably have to rename your input fields to something like answer_Q_A aka answer_1_1, answer_1_2, answer_2_1 and then process into an array from there.
<cfset answerArray = arrayNew(2)>
<cfloop collection="#form#" item="thisField">
<cfif left(thisField,6) eq 'ANSWER' and listLen(thisField,'_') eq 3>
<cfset answerArray[listGetAt(thisField,2,'_')][listGetAt(thisField,3,'_')] = form[thisField]>
</cfif>
</cfloop>
Upvotes: 1