Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

assign value to a select control from a query result

Engaged ,
Jul 31, 2013 Jul 31, 2013

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,

848
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Aug 01, 2013 Aug 01, 2013

@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#"</

...
Translate
Guide ,
Jul 31, 2013 Jul 31, 2013

@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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 31, 2013 Jul 31, 2013

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 01, 2013 Aug 01, 2013

@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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 01, 2013 Aug 01, 2013
LATEST

Thanks a million for helping,

Regards,

Iccsi,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources