The Importance of HTMLEditFormat()

I got a call today from a user who was having a weird problem when trying to save data on a form. Upon inspection, it turned out that there were a set of double-quotes in the data.

So why is this a problem? As far as ColdFusion cares, it's not, and the data was actually getting saved to the database correctly. The problem appeared when doing an edit. Everything after the first double-quote was being cutoff.

Upon further inspection of the HTML being generated, it seems that is where the problem came in. ColdFusion was doing it's job by outputting the double-quotes, with no knowledge of where in the page that data is being used. And if you use it in a form, you've got problems because it will appear to the user that the data is not saving.

The solution is to use HTMLEditFormat() when outputting values into form fields. This will cause any HTML special characters to change to their entity notations. So the HTML that is generated will actually look something like this:

 <input type="text" name="whatever" value="some "quoted" text" />

but it will display on the form as normal double-quotes, and will submit as that too:

This may be common knowledge to many of you, but I'd bet there's a whole bunch of developers who don't even realize this could be an issue. Hopefully this will help prevent some hard-to-track bugs!

TweetBacks
Comments
Kevin Penny's Gravatar Great post -
Yes I'm amazed at the projects that I am on where they don't take that into account when building forms. It's also worth noting, that when you're using the cfinput suite of tags, you don't have to worry about htmleditformat() as it's built into the tag.

Thanks!
# Posted By Kevin Penny | 8/24/07 9:50 PM
Joshua Rountree's Gravatar Great post!
# Posted By Joshua Rountree | 11/13/07 11:57 PM
Jeff's Gravatar I just ran across this same problem with an app someone else wrote. This function solved the issue quickly.
# Posted By Jeff | 1/14/08 1:36 PM
Dan Sorensen's Gravatar I have run into a separate, but similar problem: If I use htmlEditFormat() to re-edit the form multiple times, I've had the & be encoded multiple times. &amp;&amp;&amp; etc. Has anyone else run into this? How do you prevent it?
# Posted By Dan Sorensen | 6/25/08 11:41 AM
Andrew's Gravatar Just do it when outputting to the screen, not when inserting/updating
data in the database otherwise you'll get the double knock-on effect.
# Posted By Andrew | 6/4/09 10:33 AM