ASP is better than ColdFusion (Not really!)
April 07, 2008
OK, I'll start with a shocking statement...
When I started out developing websites there were 3 things in Classic ASP that I liked better than ColdFusion:
- Functions / Subroutines and Classes
- The Option Explicit declaration
- Form Variables don't have to be defined
Now that's out the way, I don't really like ASP better than ColdFusion, let me explain...
Back in those days ColdFusion was version 4.5. Functions were implimented by doing <cfmodule> calls which was really messy.
I like Option Explicit because it ensures that all variables are declared before you can reference them, this means that you can't have a typo messing up your whole web application.
Third and finally the fact that I could build a form and simply reference the key name in the form collection without having to set a default value was a real time saver, when I wanted to load the submitted values back into the form (when validation has failed you don't want the user to have to re-enter everything).
Example ASP code:
<%
IF Request.Form.Count <> 0 THEN
' process form submission...
END IF
%>
...
<input type="text" name="foo" value="<%=Request.Form("foo")%>" />
To do this with ColdFusion you need to do:
<cfif StructKeyExists(form, "foo")> <!--- process form submission... ---> <cfelse> <!--- set blank form values... ---> <cfset form.foo = "" /> </cfif> ... <input type="text" name="foo" value="#form.foo#" />
You really should be using the HtmlEditFormat() function to escape quotes (and XSS attacks) giving you:
<input type="text" name="foo" value="#HtmlEditFormat(form.foo)#" />
To replicate the ASP way, I've created a function that checks to see if the form value exists and if it doesn't just returns a blank value. While I was at it I decided to handle the issues of quotes being put into the form value which I mentioned above to stop the form breaking when your form reloads.
<cffunction name="GetFieldValue">
<cfargument name="Collection" required="true" type="struct" hint="This is usually the form collection" />
<cfargument name="Key" required="true" type="string" hint="The key in the collection" />
<cfif StructKeyExists(Arguments.Collection, Arguments.Key)>
<cfreturn HtmlEditFormat(Arguments.Collection[Arguments.Key]) />
<cfelse>
<cfreturn "" />
</cfif>
</cffunction>
So now all I have to do is:
<input type="text" name="foo" value="#GetFieldValue(form, "foo")#" />
For fusebox developers then you can also do this:
<input type="text" name="foo" value="#GetFieldValue(attributes, "foo")#" />
- Posted in:
- ColdFusion
2 comments
Leave a comment
If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

<cfparam
name = "form.foo"
default = "">
and then on your input just use
<input type="text" name="foo" value="#form.foo#" />
Wouldn't that be the same thing you are trying to accomplish with all that extra code?
Comment by Fernando Lopez – July 07, 2008
If you use form fields then you have to use something like HTMLEditFormat to stop XSS attacks or double quotes breaking your pages HTML. So you straight away you now have:
<input type="text" name="foo" value="#HtmlEditFormat(form.foo)#" />
The first time through you don't have form.foo defined so as you correctly say you could use a cfparam to create it. The trouble with this is that if you have a form with 20+ fields (which I often do) you have to type in cfparam (or the equivilent code) 20+ times. As a result I decided that as I needed to have the HTMLEditFormat function around my value anyway, I might as well create my own function that also created the variable if it didn't exist. I can then use my function (which is automatically included for every page by my framework) on all pages with forms. This actually reduces that amount of typing I have to do (by removing the need to type in cfparam 20+ times).
I hope I've explained the method behind my madness! :)
Comment by John Whish – July 07, 2008