Skip to main content
December 19, 2011
Question

Why can't I alter this variable... or find it in any scope?

  • December 19, 2011
  • 2 replies
  • 901 views

<cfif action is "preview" >

    CLEC Name:

    <input type="text" name="form_clec_name" value="#clec_name#" size="35"><br />

<cfelse>

     <cfset clec_name = "#form.form_clec_name#">

    #clec_name# || #form.form_clec_name# || #variables.clec_name#

</cfif>

I'm very new to Coldfusion (usually work in PHP), and it has mostly made sense so far, but this is driving me nuts.  All I'm trying to do is set the clec_name variable equal to an incoming form value, but it will not seem to listen to me. In the else condition in the block above, the output is always 'Original value || changed value || changed value'.  So, if I specify the variables scope before clec_name I get the right value, which leads me to believe the one I really want is in a different scope - but I have no idea how to find it or set it.  I used a short function I found online to dump all variables from every scope.... and it's not in there, even though it's clearly set and has a value.

I'm hoping whatever this is will be obvious to the Coldfusion pros - I'm hoping not to have to hunt down where the variable is created in the first place, since this is a HUGE and poorly written application.  Any help is most appreciated

This topic has been closed for replies.

2 replies

Inspiring
December 20, 2011

I used a short function I found online to dump all variables from every scope.... and it's not in there, even though it's clearly set and has a value.

I would say this is a case of the function you used not dumping out all the scopes.

Can you post the code of the function?

Which version of CF are you on?

What is the context of this code?  Is it in a CFM that is called either directly or as an include of another CFM called directly?  Is it in a CFC method?  A custom tag / module call?

--

Adam

ilssac
Inspiring
December 19, 2011

To make sure you understand what you seem to understand from your post.

<cfset clec_name = "#form.form_clec_name#">

This line is setting the clec_name variable in the local variables scope, i.e. variables.clec_name.  But this is NOT the variable you want set?

When you have an unscoped variable, ColdFusion searches it scopes in the following order.

Evaluating unscoped variables

If you use a variable name without a scope prefix, ColdFusion checks the scopes in the following order to find the variable:

  1. Local (function-local, UDFs and CFCs only) 
  2. Arguments
  3. Thread local (inside threads only)
  4. Query (not a true scope; variables in query loops)
  5. Thread
  6. Variables
  7. CGI
  8. Cffile
  9. URL
  10. Form
  11. Cookie
  12. Client

As you can see the variables scope is the sixth in the list.  That says that you are getting a different value for the unscoped variable means it must be existing in one of the first five scopes.

But what is some what unusual about that is that all those scopes are rather situational.  So if you are not inside a query loop, a <cfthread...> block or a <cffunction...> block those five scopes would be very unusual. 

Does that help any?

December 19, 2011

It helps me narrow the search, but also broadens the mystery, since I'm not in any of the situations you described .

Fortunately, it turns out that I don't need to modify that variable, as it first seemed -  I can use the form value for one small purpose and then forget it.  I would like to solve the mystery for my own benefit, but at least it's not a pressing issue anymore!