This question came over Twitter today so I thought I'd address it in a blog. User @brooksfolk asked if there was a way to get the original form field names sent to a ColdFusion file. When you look at form.fieldnames, or just cfdump the Form scope, the values are always uppercased. Consider the following simple form.
<cfdump var="#form#">
<form method="post">
<input type="text" name="naMe"><br/>
<input type="text" name="AGE"><br/>
<input type="text" name="foo"><br/>
<input type="submit">
</form>
As you can see, each of my three fields uses a different mixture of upper and lower case names. But when I view the dump, I see this:
As you can see, everything is now uppercase. I had a hunch though that getHTTPRequestData might have the "real" data and indeed it does. Here is what a dump of that function shows:
Woot - as you can see - both the original form field names and values (which are blank in this case since I didn't bother to type anything in) are in the content key. If I actually type some values in, I get a more realistic version: naMe=cat+man&AGE=moo&foo=mooooo. This looks easy enough to parse. Here is what I came up with:
<cfset content = getHTTPRequestData().content>
<cfset formMoreGooder = {}>
<cfloop index="pair" list="#content#" delimiters="&">
<cfset name = listFirst(pair, "=")>
<cfset value = urlDecode(listLast(pair, "="))>
<cfset formMoreGooder[name] = value>
</cfloop>
Basically I just treat the data as 2 sets of lists. Each pair is delimited by an ampersand and then each pair itself is split by the equal sign. The values are url encoded but luckily ColdFusion provides a way to reverse that. And here is the result...
Hope this is helpful to someone.
Archived Comments
Hi Ray nice post, and a nice thought...
I have a question about processing logic, suppose the formfield contains a field called "company" and if I enter company value in form as "AT&T", will the above logic work? If not how do we make the logic as it allows &...
Thanks
I just needed this *today*. I was writing an interface using Authorize.net's automated billing system. This system was case sensitive, so it wasn't as simple as accepting a form, validate/scrubbing it, then passing it to the service. A full mapping needed to take place.
This function saved me much headache!
Note: I wouldn't allow this, but say you had two form names like Name and name. Referencing them without going further into java methods can only return one of the above...
so, struct.Name or struct.name will only return one of the above. My short tests always yielded the lower case one.
Maybe we need the help of Form structure to identify the exact fields and values....
@Sooraj the field values come out encoded so AT&T becomes AT%26T
Thanks Rex for pointing it out...
@Brad: See this blog entry: http://www.coldfusionjedi.c...
Any idea how to get case sensitive names in multipart/form-data? seems you can get to the names through form.getPartsArray() but they are still uppercased??!
No idea on that one. If you look at the comments in the post I linked to in the comment above, it looks like a dead end.
Queries return a columnlist all in caps too.. if you serialize that to JSON, and then use the provided columnlist to get to the data.. it doesn't work.. at least in this library I am using for Objective-C.. do you know of any way to preserve the case in a query returned?
Actually.. i just noticed.. it's not the query.. it's the conversion to JSON...
If you need the case, then just copy it.
result.data = thequery;
result.collist = thequery.columnlist;
return result;
havebeer;
aye.. sometimes the easiest solutions... yeah right there in front of you..
cheers mate..
If the value is empty, it puts in the field name, not the value. The listlen is 1, not 2 when it's empty (at least on CF9). I added the following check:
[cfif listlen(pair, "=") eq 1]
[cfset value = ""]
[cfelse]
[cfset value = urlDecode(listLast(pair, "="))]
[/cfif]
Nice find there, Aaron. Thank you.