3 Replies Latest reply on Sep 23, 2011 3:37 PM by Dan Bracuk

    question on scopes

    funandlearning333 Level 1

      Hi All-


      I have a question on variable scopes. I have the following scenario.


      Suppose I have a dropdown as below with some tax years:


      <form name="frm" method="post" action="results.cfm">

      <select name="dd_year">

           <option value="2000">2000</option>

           <option value="2001">2001</option>

           <option value="2002">2002</option>

           <option value="2003">2003</option>



      <input type="submit" value="Submit" name="sbtbutton" />




      So once the user logins and selects a year from the drop down, upon clicking the submit button, the year value should be available across all pages. What is the best way to store the value? I mean in which scope?



        • 1. Re: question on scopes
          BKBK Adobe Community Professional & MVP

          On the action page results.cfm:



          <cfif isdefined("form.dd_year")>

          <cfset session.dd_year = form.dd_year>



          This will make the year value:

          1) available to all pages;

          2) specific to a particular user session.

          • 2. Re: question on scopes
            insuractive Level 3

            Also, if you need the value to be available to client-side code (e.g. JavaScript), you can also write the value to a variable in the Cookie scope using <cfcookie>.  Though its best to stick to the session scope if the variable just needs to be accessible via CF code (no reason to clutter up the cookie space unless its absolutely neccessary)

            • 3. Re: question on scopes
              Dan Bracuk Level 5

              Consider passing the value as a url variable on each page request.  Session variables are convenient but the way browsers work these days they can lead to unexpected results.


              In your case, this could happen.  The user selects 2002 on the form and goes into the site.  He then opens a new tab in his browser, goes to your form and submits 2004.  Guess what happened to the session variable on tab where he selected 2002.