Skip to main content
May 15, 2008
Answered

Variable is being run twice, and is "not defined" the second time around!

  • May 15, 2008
  • 5 replies
  • 511 views
Hello~

I have kind of a weird problem going on. I am passing in a URL variable from the first page of an application to the second one. I know it is passing correctly onto my second page, because I can output it. However... The variable is set at the top of the page in the <head> tags, above and outside a <cfif isDefined ('form.submit')>... do this</cfif>. The form itself is in the body of the page. So, when I navigate to the form page, my variable displays, but then when I hit the submit button, it tells me that same variable is not defined, even though it's not a part of the cfif isDefined function, and has already been successfully defined when the page loaded initially. Does this make any sense? Please help!

    This topic has been closed for replies.
    Correct answer tmschmitt
    I'm having a little trouble following you on this since you are only showing bits and pieces of your code, but...
    It appears to me that firmID is not getting submitted in the form. You don't show your form code, so I'm guessing.
    Can you confirm that formID is indeed a value inside the form tag?

    I would suggest highly that you scope your variables. That prevents a lot of confusion and can make code a lot easier to understand. Things like <cfset firmID = #firmID#> really serve no purpose, but <cfset variables.firmID = #form.firmID#> could possibly be useful.

    Once you get this figured out, I would also suggest researching the use of the <cfqueryparm> tag.

    5 replies

    Inspiring
    May 16, 2008
    Just so you know, while cfqueryparam does many good things, protecting you from cross site scripting is not one of them.
    Inspiring
    May 16, 2008
    I would also mention that if you are passing the value via a URL variable, you should definitely try and add some parameter security before you use it in a SQL statement.

    SELECT *
    FROM firms
    WHERE
    firms.id = #firmID#

    is highly vulnerable to SQL injection. Try instead:

    SELECT *
    FROM firms
    WHERE
    firms.id = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#firmID#">

    that should give you some basic protection.

    From what you've described as your problem, it sounds like Dan's suggestion is most likely the solution. You said you passed a URL variable from Page1 to Page2, then submit a form on Page2 to Page3. unless you pass the variable as a URL variable in the form's action attribute or as a hidden field in the form, the variable won't be present on Page3.


    Inspiring
    May 16, 2008
    quote:

    Originally posted by: semi star gazer
    Hello~

    I have kind of a weird problem going on. I am passing in a URL variable from the first page of an application to the second one. I know it is passing correctly onto my second page, because I can output it. However... The variable is set at the top of the page in the <head> tags, above and outside a <cfif isDefined ('form.submit')>... do this</cfif>. The form itself is in the body of the page. So, when I navigate to the form page, my variable displays, but then when I hit the submit button, it tells me that same variable is not defined, even though it's not a part of the cfif isDefined function, and has already been successfully defined when the page loaded initially.

    My guess is that you didn't do anything to pass the variable from page 2 to page 3. Page 3 only knows about what page 2 sent it. It knows nothing about what page 1 sent page 2.
    tmschmittCorrect answer
    Participating Frequently
    May 15, 2008
    I'm having a little trouble following you on this since you are only showing bits and pieces of your code, but...
    It appears to me that firmID is not getting submitted in the form. You don't show your form code, so I'm guessing.
    Can you confirm that formID is indeed a value inside the form tag?

    I would suggest highly that you scope your variables. That prevents a lot of confusion and can make code a lot easier to understand. Things like <cfset firmID = #firmID#> really serve no purpose, but <cfset variables.firmID = #form.firmID#> could possibly be useful.

    Once you get this figured out, I would also suggest researching the use of the <cfqueryparm> tag.
    Inspiring
    May 15, 2008
    semi star gazer wrote:
    >
    > Variable Set:
    >
    > <cfset firmID = #firmID#>
    >

    This is a really weird line of code. What are you trying to do here?

    You are setting the variable firmID to the value of the variable firmID.
    Doesn't that seem a little circular? I suspect you are having a
    scoping issue and this may or may not be working because of CF generous
    nature to scan many scopes for a variable name if you do not provide the
    scope to be used. But I don't know enough about your code to say for sure.

    May 15, 2008
    You're right, it is repetitive, but unfortunately removing it didn't solve my problems! The variable still displays when the user gets to the page, but I get the " Variable FIRMID is undefined" upon submit. The query that it is hanging on is not inside the <cfif is defined form.submit> tags, so i'm not even sure why it is running again. I am assuming it's something about how I have the query laid out, but I don't know what to change!