4 Replies Latest reply on Aug 1, 2013 3:25 PM by iccsi

    assign value to a select control from a query result

    iccsi Level 1

      I want to assign a value to my controls from my query result.

      It works for CFINPUT TEXT, but it does not work for SELECT

      I would like to know are there any way tp assign my dropdown list value base on the questy result.

       

      I have CFSTOREPROC like following:

       

      <cfstoredproc procedure="MySP">

          <cfprocparam value = "#form.IDNumber#" CFSQLTYPE = "cf_sql_integer">

          <cfprocresult name="spResult" resultset="1">

        </cfstoredproc>

       

       

      <CFINPUT type="text" id="txtIDNumber" value="#spResult.IDNumber#" /> 

       

      I am able to siign the value to a CF INPUT TEXT like above code,

       

      <select id="lstNumber" value="#spResult.lstNumber#"></select>

       

      but it does not assign the value from my query result for SELECT input.

      Are there any way to assign a value to SELECT control from query result?

      I tried to use JavaScript which works if I pass a number, but it does not work if I pass a query result,

       

      Your help is great appreciated,

       

      Regards,

       

      Iccsi,

        • 1. Re: assign value to a select control from a query result
          Carl Von Stetten Adobe Community Professional & MVP

          @Iccsi,

           

          With a SELECT input, you set the "selected" attribute on one of the OPTION tags inside of it to set its value.  Given this pseudo code:

          <select id="mySelect" name="mySelect">

               <cfloop query="myQuery">

                    <cfif myQuery.someValue = "myDesiredDefaultValue">

                         <option value="#myQuery.someValue#" selected="selected">#myQuery.someValue#</option>

                    <cfelse>

                         <option value="#myQuery.someValue#">#myQuery.someValue#</option>

                    </cfelse>

               </cfloop>

          </select>

           

          Assuming you are looping through the results of the stored procedure you showed in your code and populating the SELECT with those values, you have to check on each loop iteration if the value you want to be the default is the value you get from that row of the query; if it is, you add the "selected" attribute to the OPTION tag, otherwise you don't.

           

          HTH,

          -Carl V.

          • 2. Re: assign value to a select control from a query result
            iccsi Level 1

            Thanks for the message and help,

             

            I tried to use following code, the SELECT select the right one, but the list becomes the text, not from query.

            Once I add CFOUTPUT then the the list repeat many times, since it is in loop.

             

            Thanks agin for helping,

             

            Regards,

             

            Iccsi,

             

            <select id="mySelect" name="mySelect">

                 

                 <cfloop query="Ratings">

                  <!---<cfoutput query="Ratings">--->

                      <cfif Ratings.RatingID EQ form.RatingID>

                           <option value="#RatingID#" selected="selected">"#Rating#"</option>

                      <cfelse>

                           <option value="#RatingID#">"#Rating#"</option>

                      </cfif>

                 <!--- </cfoutput>--->

                 </cfloop>

                

            </select>

            • 3. Re: assign value to a select control from a query result
              Carl Von Stetten Adobe Community Professional & MVP

              @Iccsi,

               

              My bad!  Yes, you will either need the CFOUTPUT tag (as you have commented out in your code) but without specifying a query, or change the CFLOOP to CFOUTPUT query="Ratings".  So either of these should work:

              <select id="mySelect" name="mySelect">

                   <cfloop query="Ratings">

                    <cfoutput>

                        <cfif Ratings.RatingID EQ form.RatingID>

                             <option value="#RatingID#" selected="selected">"#Rating#"</option>

                        <cfelse>

                             <option value="#RatingID#">"#Rating#"</option>

                        </cfif>

                   </cfoutput>

                   </cfloop>

              </select>

               

              OR

              <select id="mySelect" name="mySelect">

                   <cfoutput query="Ratings">

                        <cfif Ratings.RatingID EQ form.RatingID>

                             <option value="#RatingID#" selected="selected">"#Rating#"</option>

                        <cfelse>

                             <option value="#RatingID#">"#Rating#"</option>

                        </cfif>

                   </cfoutput>

              </select>

               

              -Carl V.

              • 4. Re: assign value to a select control from a query result
                iccsi Level 1

                Thanks a million for helping,

                 

                Regards,

                 

                Iccsi,