Solved

Populate <cfselect> with query data

Posted on 2015-02-11
3
120 Views
Last Modified: 2015-02-13
I am trying to populate my form with data returned from my query .... <cfinput> are populating just fine but <cfselect> i can not get to populate ... I'm using coldfusion 11 ... and to populate the <cfinput value =querydata.field... but that doesn't work for select ... I have a yes no and I want it to switch to the value returned (for example No then it should show no in case I wanted to update and change to yes ...  is there a simple way to do this
<cfform >
	

              </p>
   	      <label for="email">Email Address</label>
	     <cfinput type="text" name="email" message="Please enter an email address." value="#qEditRoles.email#" 
				validateat="onSubmit" validate="email" required="no" id="email" size="50" maxlength="50">
		</p>
		       	   <cfselect name="admin_priv_select" id="admin_priv"size="3">
		            <option value="1">Yes</option>
		            <option value="0">No</option>
		            </cfselect>
		       	  </p>
</cfform>

Open in new window

0
Comment
Question by:dolamitejenkins
3 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 40605408
You're not using any features of CFSELECT so might as well just use SELECT, but either way, you need to add a condition CFIF to determine which option should be selected like this..

What if the value is empty in the database (such as with a new record?)  YOu need to default it to one of the values or add a third option like this..

		       	   <select name="admin_priv_select" id="admin_priv"size="3">
		            <option value="" <cfif qEditRoles.admin_priv_select is "">selected</cfif>>-Choose-</option>
		            <option value="1" <cfif qEditRoles.admin_priv_select is "yes">selected</cfif>>Yes</option>
		            <option value="0" <cfif qEditRoles.admin_priv_select is "no">selected</cfif>>No</option>
		            </select>

Open in new window

0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 40606363
If your database permits only two values Yes or No,  then may be you do not use cfselect, use radio (which may default to "no".

But answering you question, you can populate cfselect with query. You can set the selected value too.

see example:

<cfset qData = QueryNew ("value,display","varChar,varchar" )/>
<cfset t = QueryAddRow(qData)/>
<cfset t = QuerySetCell(qData,"value","no")>
<cfset t = QuerySetCell(qData,"display","No")>
<cfset t = QueryAddRow(qData)/>
<cfset t = QuerySetCell(qData,"value","yes")>
<cfset t = QuerySetCell(qData,"display","Yes")>
<cfset user_is_admin = "no">

<cfform name="f1">
      <cfselect name="sel1" value="value" display="display" query="qData" selected="#user_is_admin#" queryPosition="below">
            <option value="">Select Option</option>
      </cfselect>
</cfform>
0
 

Author Closing Comment

by:dolamitejenkins
ID: 40608382
thank you
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now