6 Replies Latest reply on Jan 31, 2010 5:07 PM by -==cfSearching==-

    get a dynamic cfselect to remember a selected value

    cfsetNewbie Level 1

      Hello;

      How do I get a cfselect to remember a value if a user has selected one of the choices, but forgets to fill out another form field?

       

      here is my select:

       

      <cfselect enabled="No" name="state" size="1" class="formSelect" multiple="no">
                      <option value="">--Select a State--</option>
                      <cfloop query= "SESSION.stat">
                        <option value="#state#">#state#</option>
                      </cfloop></cfselect>

       

      Everything works accept for remembering what a user chooses I usually use something like value="#form.state#" but it doesn't work in this insance. Can anyone help me? I can't figure this out.

       

      thanks

        • 1. Re: get a dynamic cfselect to remember a selected value
          Dan Bracuk Level 5

          Step 1 - do a query of queries to get ---Select a State--- as the first row of that query.

           

          Step 2  - in your cfselect, use the query and selected attributes instead of the loop.

          • 2. Re: get a dynamic cfselect to remember a selected value
            cfsetNewbie Level 1

            I have to use a loop, it's embeded inside a cfoutput and I don't need to use he q

            uery to set up the form. it goes like this:

             

            <cfquery name="SESSION.stat" datasource="#APPLICATION.dataSource#" cachedwithin="#CreateTimeSpan(0, 6, 0, 0)#">
            SELECT state, ID
            FROM States
            </cfquery>

             

            then the form:

             

            <cfoutput>

            <cfform action="#cgi.script_name#" method="post">

            <cfinput type="text" name="name" id="name" value="#form.name#">

             

            My select:

            <cfselect enabled="No" name="state" size="1" class="formSelect" multiple="no">
                            <option value="">--Select a State--</option>
                            <cfloop query= "SESSION.stat">
                              <option value="#state#">#state#</option>
                            </cfloop></cfselect>

            </cfform>

            </cfoutput>


            How would I make this work and remember what they selected if they forgot to type in their name? (There are a bunch of fields in this form, I just pulled the one so you see what's going on.)

            • 3. Re: get a dynamic cfselect to remember a selected value
              -==cfSearching==- Level 4

              I have to use a loop, it's embeded inside a cfoutput

               

              No, you do not "have to" use a loop. As Dan suggested, you can use "the query and selected attributes" of cfselect. This replaces the cfloop. CF uses the query and column names you supply to populate the select list automatically. Likewise, it uses whatever "selected" attribute you supply value to pre-select the desired list option.

               

              <cfquery name="SESSION.stat"

              datasource="#APPLICATION.dataSource#"

              cachedwithin="#CreateTimeSpan(0, 6, 0, 0)#">

               

              What is the benefit of storing the query in the session scope, on top of caching the query?

              • 4. Re: get a dynamic cfselect to remember a selected value
                Adam Cameron. Level 5

                What is the benefit of storing the query in the session scope, on top of caching the query?

                 


                Especially as the query is pulling out a list of states.  It's not like the recordset is going to be different from one person (session) to the next..?

                 

                Also: probably best to ORDER that list of states by something...

                 

                --

                Adam

                • 5. Re: get a dynamic cfselect to remember a selected value
                  cfsetNewbie Level 1

                  That fixed it, thank you! I was still working the old way. back in 5. Forg

                  ot 8 is so much easier to use in some respects. Works noce now. Thanks again

                  • 6. Re: get a dynamic cfselect to remember a selected value
                    Adam Cameron. Level 5

                    I think Dan (rather than me, as you've marked it) probably answered your question here, but not to worry: at least you got there in the end.

                     

                    If you're coming from CF5, it might be a good idea to go to the CF8 docs and read the bit on "what's changed since CF5", just as a refresher.

                     

                    Best of luck.

                     

                    --

                    Adam