Dec 21, 2007

Variable Primer

Vocab:

Variable: a container that holds something

Value: assigned to a variable

example = variable = value

In coldfusion the simple variable can be handled in a few ways.

For example say we have the word 'apple' and we want to take this word and pass it throughout our application.

In a form this is how you would do it:

<form action="variables2.cfm" method="post">
<input name="fruit" value="apple" type="text">
<input value="submit" name="submit" type="submit">
</form>

and on the variable2.cfm page you would need code that looked like this to display it.

<cfoutput>
</cfoutput><p>passed variables</p>
<cfif>
#form.name#
</cfif>

This is a URL what we can a query string variable

<a href="http://www.blogger.com/variables2.cfm?name=frank">URL variable example</a>

For a session you would set your variable like this: Session are great for storing variables that are needed throughout your website. so instead of move hidden input fields through forms, or long crazy querystring you simple set the session.whatever and assign the value.

<cfset firstname=" 'Doug'">
<cfoutput>
#session.firstname#
</cfoutput>

Here is another type of variable called a 'request' variable. This type of variable only lasts for the life of the page...Move to another page and it is gone. request.variables are useful for database references where you would assigned the data source to something like request.dsn (datasource name).

<cfset frank=" 35">
request.frank is <cfoutput>#request.frank#</cfoutput>

The last variable is just the variable's variable where you would set your variable in somethign like this &lt;cfset fruit = 'apple'&gt; it only lasts for the duration of the page, move away from the page and it is gone.

Frank

Labels: , , , , , , ,