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

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

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

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.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.