2 Replies Latest reply on May 19, 2008 6:36 AM by Scatterblak

    cfselect onchange to set session variable

    jpollar319
      I'm trying to figure out how to take the select value from a cfselect and use it to set a session variable.

      I've tried calling a cfscript function...no success.

      I'm not using flash...and that's the only examples I could find.

      Any ideas?
        • 1. Re: cfselect onchange to set session variable
          Level 7
          javascript = client-side
          cf = server-side

          the 2 do not meet unless you submit data to the server...

          several options for you:
          1) easy way: submit your form with the select's onchange event and
          process the form data to set the session var
          2) if you are on CF8 you can use the new cf ajax features like
          cfajaxproxy to send data to a cfc
          3) if you are not on CF8, you can employ rob gonda's ajaxCFC to achieve
          similar functionality
          4) use a js framework like jQuery which supports asynchronous requests
          to send the selected value to a cfm/cfc

          option 1) above will require page reload. other options can do it
          asynchronously without reloading the page, but you better know your
          javascript... and cf components...

          Azadi Saryev
          Sabai-dee.com
          http://www.sabai-dee.com/
          • 2. Re: cfselect onchange to set session variable
            Scatterblak Level 1
            If you want to do it without using the built in ajax stuff, you can still roll your own. You'd need a hidden frame, like an iFrame set to 0 height and width, and a backend template the accept the information from the generated select box (I'll call this 'myVariableProcessor.cfm'). Name the hidden frame 'theProcessor' or something, and make sure your form has a name as well. In the javascript event of the CFSelect-generated select box, you need something like this: "Javascript:goSetMyVariable();". Then, in the same page, you'll need a javascript function something like this:

            function goSetMyVariable(){
            mySel = document.all.myForm.mySelectBox;
            myVal = mySel.options[mySel.selectedIndex].value;
            document.all.theProcessor.src='myVariableProcessor.cfm?newValue='+myVal;
            }

            ...the upshot is, when the onChange event fires in your CFSelect box, you call a javascript function that grabs the relevent value out of the CFSelect, and then passes it to a processor template that updates your session variables. Do all this in a hidden frame, and it should remain invisible to the end user. I'd suggest at least using ssl for this, and checking authentication in the var processor, as there are some security issues associated with passing URL variables to alter session vars, but you get the idea.