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: , , , , , , ,

Dec 19, 2007

Simple session and setting sessions with cfset example

As part of my effort for creating simple Coldfusion examples. I want to take about variables. As we know in a normal website we are taught to pass URL or FORM variable from page to page. This is great for a one or two things or for a form of variables/values. So there is another very useful way to pass variables and that is by sessions. I won’t explain what a session 'is' except that it works with cfset tag like normal variables.

Let me show you

URL variables look like this when you pass it from a URL query string.

http://www.sitename.com/index.cfm?binky=boink

then you pull it into the the target page like this:

<cfoutput>
#url.binky#
</cfoutput>

You don't need the value after the equal sign because when the page resolves it will show on the screen.

imagine a form on a page that has a textbox input area. The label of the this 'input' text field is called 'name'. Then you type in the word 'frank' and hit submit.

It passes the value to a page called formCatch.cfm

You would display it like this.

<cfoutput>
#form.name#
</cfoutput>

It would output the word frank.

And now lets look at a session:

<cfset session.name = 'frank'>

<cfoutput>
#session.name#
</cfoutput>

It would output the name Frank, but here is the good thing about sessions, you can go to any page after setting the session variable and call it.

So lets go back to the FORM and URL variables. those are great for one page justs, but after that they loose purpose.

sessions stay for the duration of your access to the website or until you explicitly tell it togo away.

Here is how you do that.

<cfset StructDelete(Session, "name")>

One other note. Before you can use sessions in your website you have to 'tell' it that it will use sessions.

Here is how that works.

In your web application directory you can create a blank .cfm file called 'Application.cfm'. Put this file on the directory where you will use sessions.

Then add this line of code:

<cfapplication sessionmanagement="yes">

So that is sesssions in a nutshell. This should get anyone by with sessions.

Frank

Labels: , , , , , , ,