Jump Start To ColdFusion MX - Form Handling
(Page 5 of 6 )
When a form is submitted, ColdFusion places all of the form variables in the 'form' scope. If I had a form field named 'username', I would access this value like this:
#form.username# For the record, you do not have to use the scope to gain access to this variable, although using the scope is a good programming practice. ASP and PHP developers will be familiar with this content for the Request and $_POST collections respectively.
ColdFusion also places all form values submitted into a structure. This is very helpful when building dynamic forms. Lets go over a simple code example:
Place this in a page called myname.cfm:
<form action="test.cfm" method="post">
Enter Your Name:<input type="text" name="myname" size="30"><br>
<input type="submit" name="go" value="Submit Name">
</form> Here is test.cfm:
<cfif IsDefined('form.myname')>
Hello <cfoutput>#form.myname#</cfoutput>
<cfelse>
Please enter your name.
</cfif> When the form is submitted the if block will see that the variable 'form.myname' is present and it will output 'Hello Your Name Here'. Pretty simple isn't it?
Conditional Statements Let’s now look at using conditional if statements. I just used if and an else in the previous coding example. Using if statements in ColdFusion is very easy. The tag syntax is as follows:
<cfif expression goes here>
code to execute if the if statement is true
</cfif> The if statements can get much more complicated than the example I just showed you, but you get the idea. You can also have else's and elseif's in there as well. Here's a short example:
<cfif IsDefined('form.myname')>
Hello <cfoutput>#form.myname#</cfoutput>
<cfelseif Not IsDefined('form.myname')>
Please enter your name.
<cfelse>
Something else goes here.
</cfif>Next: Conclusion >>
More ColdFusion Articles
More By Clint Tredway