Transferring Data

There are two primary ways to transfer data from page to page in ColdFusion. Embedding a variable in the url, or posting the data with a form are excellent ways to transfer data such as a variable from page to page.

Using Links to Transfer Variables

Links are very effective at this, but please try not to put more than 2 or 3 variables in a link. It is extremely tacky and everyone likes a nice clean url. Below are examples of how to use a link to transfer data.

Example
//Page 1
<a href="test.cfm?myVar=427">Loop Statement</a>
//end Page 1

//Page 2
<cfparam name="myVar" default="" type="integer">
<cfoutput>
    #myVar#
</cfoutput>

Pretty awesome, huh? You just transfer variable data through a link. ColdFusion is smart enough to understand where the page name ends and the url parameters begin.

The extremely important <cfparam> tag

Go ahead and type some numbers in the url after “myVar=”. If you tried being smart and typing in letters, you should now have an error. The type allowed for our variable was set in the <cfparam> tag. Basically, what happened here was that you put a variable in the url and the <cfparam> tag went looking for anything with the name of myVar to set its value too.There are like 20-30 actual types that the <cfparam> can hold, but here are some of the important ones.

  • boolean
  • integer
  • string
  • list

Of course, Adobe has an awesome tutorials on the <cfparam> tags. Just go to their website, and look it up in the “ColdFusion Tags” section.

Using Forms to Transfer Data

Forms can transfer data in two ways, through a GET or POST action.

GET Request

Example
<cfparam name="getTextBox" default="" type="String">
<cfoutput>
    <form action="ColdFusionTransferring.cfm" method="get">
        <input name="getTextBox" type="text" value="Type a string here..." onfocus="$(this).val('')"/>
        <input name="" type="submit" value="Submit" />
        You typed: #getTextBox#
    </form>
</cfoutput>

Notice how the url has the variable in it after you submit the form because the following POST Request will not. POST Requests are still inside the HTTP Request, but they are not displayed in the url. You can use FireFox’s FireBug or Chrome Developer Tools to see these requests and the post values.

POST Request

Example
<cfparam name="postTextBox" default="" type="String">
<cfoutput>
    <form action="ColdFusionTransferring.cfm" method="POST">
        <input name="postTextBox" type="text" value="Type a string here..." onfocus="$(this).val('')"/>
        <input name="" type="submit" value="Submit" />
        You typed: #postTextBox#
    </form>
</cfoutput>

If you clicked submit on the GET Request and then clicked submit on the POST Request right after, you will notice that the GET Request is actually cleared from the url and doesn’t appear in page. This is because every time we are clicking submit we are sending the forms contents and putting them into temporary variables for the next page. So, when we did the POST request after the GET Request, we lost our GET variables because we weren’t sending them to the next page.

GET/POST Etiquette

So, why don’t we just use GET Requests for everything? I mean it would have the variables up in the url where we could see them. It would help with debugging. While that is great, GET Requests make the url look tacky, and a url can only be so long. That means if you had enough variables in your GET Request to surpass the url length limit, you wouldn’t actually get all of the variables. So, please try to stick with POST requests unless you need the user to be able to show other user’s that specific state through sharing a link. It is recommended that POST Requests are when you want to modify data, and GET Requests are to show data.

Try and Catch Statement

ColdFusion provides a try and catch statement to deal with error handling. Basically, a try and catch statement attempts some code. If the code fails, ColdFusion will do whatever is in the exception to try to handle it without breaking and spitting out an error message to the screen. Of course, many different types of exceptions can occur, which should sometimes be handled in a different manner than the others. Let’s take a look at the general syntax of a try and catch statement in ColdFusion.

Example
<cftry>

  <!—Code tried —>
  <cfcatch type=”Exception_Type”>

    <!—Code executed when the code above fails—>
  </cfcatch>
</cftry>

So, we can see that it isn’t very complicated. You simply have a try and a catch. In the try, you attempt the code that might fail. The catch code is only triggered when that happens. The idea for the catch code is to handle the error and not just to stop ColdFusion from showing an error message. You actually want to correct something in the catch statement.

When to use a try/catch statement

Some people don’t want to use try/catch. They think that if there is a chance the code might not work, they shouldn’t use the code at all. So, what are a couple of examples of why this would be useful? Let’s say you have some code that updates a database with extremely important information for your manager. So, if that code fails your manager would never get any of the information derived from that code. However, if you would have used a try and catch statement, you could have made ColdFusion send an email with the information whenever it failed. This would let your manager have information that he wouldn’t have without this powerful little function. You can also use try and catch to improve the exception reporting from ColdFusion. A custom message from the web developer is usually better than a default error message.