Array Notation vs. Evaluate()

While developing a web site for a customer, I recently had the problem with evaluating a form field to retrieve that field's value. For example:

I had a form field as follows:

<input type="text" name="q_1_f35d5381-c9f7-431e-6332-4d404f55ed4c" id="q_1_f35d5381-c9f7-431e-6332-4d404f55ed4c" size="25" value="" />

As you can see I'm using a GUID in the name. This helps me map the form back to another entity after processing.

In order for me to get the value of the field, I loop through the fieldnames and Evaluate() that field as follows:

<cfloop list="#frm.fieldnames#" index="x">
<cfset s.value = Evaluate("frm.#x#") />
</cfloop>

However, when I submit the form and run the code, I received this CF error:

"4d404f55ed4c," on line 1, column 33, is not a valid identifer name.
The CFML compiler was processing: an expression beginning with "frm.q_1_f35d5381", on line 1, column 1.This message is usually caused by a problem in the expressions structure.

It looks like CF attempts to "subtract" the values between the "-" in the GUID. In other words, its attempting to subtract 4d404f55ed4c from 6332 which are the values at the end of my GUID.

After a few hours of attempting to resolve this issue I finally posted the issue on the Adobe Forums.

After just a half-hour a kind fellow by the name of Azadi (thanks Azadi, whoever you are) provided a fantastic solution: Array notation on the structure iteself instead of using evaluate! Duh.

I've used this solution before but not on a form structure. So, I changed the code that proceses the form as follows:

<cfloop list="#frm.fieldnames#" index="x">
<cfif StructKeyExists(frm,"#x#")>
<cfset s.value = form['#x#'] />
</cfif>
</cfloop>

And it worked beautifully. Remember array notation instead of Evaluate() is a good option in this circumstance.

Comments
Contact Chris SchofieldBlogCFC was created by Raymond Camden. This blog is running version 5.9.001.