Solved

Populate <cfselect> with query data

Posted on 2015-02-11
3
126 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 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