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:


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:


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.

8 thoughts on “Posting Form to Itself

  1. 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.


  2. @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.


  3. 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.


  4. 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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s