Undefined variables

?I got the error Variable Undefined? or ?Undefined in FORM?. If you have come across this and didn?t know why then this tutorial is for you. I have been surfing the forums for about two months now and by far the most common problem is undefined variables. Yet they are the EASIEST problems to find and fix. 

To understand Variables you first need to understand scopes. ColdFusion uses to differentiate between variables. Every variable has a scope. And if you?re reading this then the scopes most persistent to you are as follows

Variables
Form
URL
CGI
COOKIE
SESSION
APPLICATION
SERVER
(This is not a complete list just the most common. To view complete list consult cf documentation)

To better understand scopes use this analogy. A scope is a room in your house and the variable is an item in your room. So for example

Kitchen.plate refers to the plate in the kitchen. 
Bedroom.plate refers to the plate in your bedroom, even though you were told not to eat there =p 

Two very different plates two very different variables but both carry the name plate, one is used in the bedroom one is used in the kitchen. You always want to define your scope or CF won?t know what plate to use when you tell it to just use plate. So don?t confuse it and always add the scope to the variable.



?But Newton I already know all that easy stuff but I still cant fix my error!? 

Ok lets break down that error shall we? 

For this example I will use the most common error I have seen 

?ERROR: PLATE is undefined in FORM on line 23? 


On the page your getting that error you have on line 23 #form.plate# but guess what, Coldfusion don?t see a plate in form. It doesn?t exist. Why don?t it exist? Good question. 

And to answer that you have to go to the root of the problem. Where do you have form.plate being defined? Or any variable for that matter. If coldfusion can?t find it then you need to find it. 

To be able to find it you need to understand a little about the scopes and where they can be used, so lets touch on that. 

Server scopes- persistent scopes that have pre-defined variables
CGI and SERVER scope variables are stored server side and are unique to them.
And already have defined names. So you probably wont ever get an undefined error in those if you make sure you spell them right. 

Non-persistent scopes ? only last for one page
FORM is not a persistent scope. Meaning the variables only exists for the page the form was sent to. After that they go die and don?t exist no more. 

URL is not a persistent scope. This scope is available as long as you got the variables defined in the URL using the ?plate=yes, with that appended to the URL then URL.plate is defined 

Variables scope is the most common scope and only lasts for the page it is on. 


Persistent scopes ? user defined variables that persist for more then one page.
Session variables are unique per user, stored on the server and persist for the length of the session. Session is defined in time by the sessiontimeout set in the Application.cfm or by the servers? default, which is usually 20 minutes. Common uses could be username user access level etc.. 

Application variables are stored on the server with the timeout defined in the Application.cfm or by cfadmin. The difference with application variables is they are global to everyone. Application.plate for someone in china visiting your site is the same value for someone in NY visiting your site. Common uses could be the sites last updated time or the sites title etc?

Cookie scope is persistent for as long as the cookie exists. This scope, unlike the previous two, is stored on the clients? pc, making it somewhat unreliable. It is also unique to the user. Common usage for these can be the username, Site theme, etc?. 

Now back to the problem form.plate is undefined. Where did it come from? Why the form of course. Or did it? Did you get to this page by submitting a form? 
I?m going to assume you already knew that and you did submit the form to get to the undefined error. So go back and take a look at your form where the variable is defined. 
You sure you got it defined? Is it spelled right? I think now your starting to get the idea. 


IN SUMMARY ? 
When you get an undefined error make sure you got the scope defined. 
Make sure the variable exists in that scope. By understanding the persistent and non-persistent scopes you should be able to determine if the variable exists for the page giving you the error. 

Another way to overcome undefined variables is with the following line

<cfparam value="scope.variable" default="<your default value>"

What that does is it tells the server that if for some reason the variable doesn?t exists use the default instead (especially useful when using checkbox?s in a form, remember unchecked boxes will be undefined) 

If you still can?t find the problem try using the following code at the top of your page
<cfdump var="#<scope name>#">
<cfabort> 


You will see a pretty little table telling you exactly what is in that scope.


All ColdFusion Tutorials By Author: Newton
  • Undefined variables
    This tutorial will help solve the most common undefined variable issues. It is written for the beginner coder in terms that make scopes easily understandable by anyone.
    Author: Newton
    Views: 14,397
    Posted Date: Saturday, September 11, 2004
Download the EasyCFM.COM Browser Toolbar!