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.
//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
Using Forms to Transfer Data
Forms can transfer data in two ways, through a GET or POST action.
GET Request
<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
<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
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.