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:


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.

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.

    Like

  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.

    Like

  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.

    Like

  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.

    Like

Leave a Reply to Ron Fuller Cancel reply

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

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s