ColdFusion in Context: Unreal Forms

When you check to see if a form has been submitted, you generally check for the presence of a specific form field that won't have a value until the form has been submitted. It's a good thing you check for a specific variable in a real form; because, there are also... pause... take a deep breath... unreal forms. (Have we missed Halloween?)

Unreal Forms

Put the following code in mystery.cfm. The results may blur your perspective of what a form really is. First, check to see if "form" is defined. If it is, then check to see if "form" is a structure. If it is, then give form.Jack a value. Check to see if form.Jack is defined. Finally, check to see if a variable you haven't named is defined.

<cfif isDefined("form")>
  Form is defined.
  <cfif isStruct(form)>
    Form is a structure.
    <cfset form.Jack=92>
  </cfif>
</cfif>
<cfif isDefined("form.Jack")>
  Jack exists for ColdFusion.
</cfif>
<cfif isDefined("form.Mode")>
  Mode is defined.
</cfif>

Browse mystery.cfm. It says that you already have a form, that form is a structure, and that the structure is so real that you can add variables to it. You know the isDefined function is really working; because, it will tell you that an undefined form variable doesn't exist. Therefore, you have an unreal form.

Real Forms

Add the following code to make a real form. Surround it with cfif tags so you can turn it on and off by simply adding a query string to the URL. Let it submit to the current page by default (not specifying an action). However, be sure to set the method to post. Give it a submit button, and give it a hidden field named Mode.

<cfif isDefined("url.Real")>
  <form method="post" name="Frank">
  <input type="submit" name="Fred" value="Click Here">
  <input type="hidden" name="Mode" value="26">
  </form>
</cfif>

Browse mystery.cfm?Real=87 (or give Real any other value you like). You see a submit button. The other results are at first just the same though. Everything you've checked for is defined except for Mode.

Press the submit button. Because you didn't specify an action, the current URL including the current query string becomes the action. Now the form has been submitted and now a field that it created exists. If you remove the query string, the page reverts to its old self.

Implications

It's tempting, once you realize that a form is a structure just like any other structure, to check for the presence of the structure to determine if a form has been submitted. However, as you've just seen, the structure always exists, even if the page doesn't have a form or even if it has an unsubmitted form. This lets you use ColdFusion to assign values to form fields that may be encountered later (if ever). In the past, you checked for the existence of a specific form field, not just the form structure, in order to know if a form had been submitted. Now you know why. =Marty=