Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Populate <cfselect> with query data

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
dolamitejenkins
Asked:
dolamitejenkins
1 Solution
 
gdemariaCommented:
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
 
Pravin AsarCommented:
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
 
dolamitejenkinsAuthor Commented:
thank you
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now