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:
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:
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 .
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.
The default action for a form is to post to itself. If I am posting a form to itself I just omit the action attribute.
LikeLike
Just confirming what Dan said.
LikeLike
Just to clarify Dan and Todd’s comment: the default action of a cfform is to post to itself. There is no default action for plain old HTML forms.
LikeLike
Just remember that cgi.script_name may not always return the expected results. You can’t always rely on CGI variables–since they’re web server specific.
I prefer using getFileFromPath(getBaseTemplatePath()) as it should work with any web server.
LikeLike
Boring. I don’t even use forms anymore.
LikeLike
@Terrance
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.
LikeLike
cgi.script_name isn’t completely unpredictable here and Apache / IIS aren’t that much of a screwball that they return randomness. I have experience running CF on both Windows / Centos, as well as Apache / IIS.
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.
LikeLike
Wow, so much traction on such a small topic.
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.
LikeLike