Andrew asks:

I hope you can help me. I come from an ASP.NET background and am moving across to ColdFusion. In .net there is a function called isPostBack() which came in very useful. I am now finding a need for this functionality within the Coldfusion environment so that certain blocks of code are only executed while the page is loading for the first time.

First off, I'm happy to welcome you to the ColdFusion community. We are a rowdy, but generally fun, bunch of geeks. We are known for being passionate about our language and emptying the beer kegs at conferences. I think you will find life much more fun here!

So I had to do a bit of digging, but I found that Andrew was a bit off on his description. isPostBack in .Net is run when a form is posted, not when a page is loaded. However in further emails with him he told me he was interested in both.

There is nothing inherit to ColdFusion that will, by itself, tell you if a form has been posted. You can check for a form post in a variety of ways though.

First, ColdFusion automatically populates a form field named fieldnames when a form is posted. This is a list of all form values. So one simple method to see if a form has been posted would be:

<cfif isDefined("form.fieldNames") and len(form.fieldNames)>

This will check to see if the value even exists and if it has something in it. I find that a bit long though. I think most people simply check for the existence of a particular form field. I will often check the name of the submit button. Given:

<input type="submit" name="save" value="Engage!">

You can do this to look for the submission:

<cfif isDefined("")>

You can also treat the Form as a structure and check for either an empty struct or a particular key:

<cfif not isStructEmpty(form)>


<cfif structKeyExists(form, "save")>

Obviously each of these could be reversed to check for a form not being submitted. One example:

<cfif not structKeyExists(form, "save")>

You could get really technical and dig into the headers. If you run getHTTPRequestData when a form post is submitted, the method will return POST as opposed to GET. That is probably going a bit too far thought.