Posting Form to Itself
I'm working on someone else's code base, and found that they were posting forms to themselves. However they had hard coded the form template names into the code for the form. Like the following:
<cfform action="index.cfm" method="post">
This is a bit of a pet peeve of mine because it tends to make the code very un-portable. What happens if you rename the file "dosomethingelse.cfm." Now you have to go back and change the file name and the reference.
It was a quick proof of concept application, so I don't fault the author. But they just aren't lazy enough.
I prefer doing it this way:
<cfform action="#cgi.script_name#" method="post">
It's highly cut and paste-able, and you never have to worry when you rename your templates. If you aren't using cfform, you can still do it, just wrap the form element in a <cfoutput>.
Now, I imagine that I will get a comment that says something about not trusting cgi variables. It works with every flavor of IIS and Apache that I have ever worked with. Anybody see any gotcha's doing that.
Comments
I prefer using getFileFromPath(getBaseTemplatePath()) as it should work with any web server.
I (mostly) agree with you and do exactly as you (and never had a problem with CGI variables doing this)
but the downside is that it takes a few cycles to get that SCRIPT_NAME whereas if it were hardcoded, there would be none.
I also wanted to confirm that Rob's confirmation of Dan / Mine's confirmation was that the default action of cfform is to post to itself is definitely the correct confirmation.
Okay, good point about the default action of cfform. I hadn't realized that. I would probably keep doing cgi.script_name as it would be consistent between cfform's and plain old forms.
As for the processing hit of getting a cgi variable. I would have to conclude that this is premature optimization, as the variables already exist.



Posted by: Dan at March 1, 2008 8:42 PM