Solved

trouble with SELECT field

Posted on 2014-04-25
4
254 Views
Last Modified: 2014-04-25
ColdFusion 9
MS SQL Server 2012

Hi. I am having a little trouble with a SELECT menu field in a form. The SELECT menu is a dropdown list of USA states.

When I edit a database record, I want the SELECT menu to display, or highlight, the value that is already in the database table. For example -- "Kentucky".

I have stopped using CFFORM and CFSELECT -- mainly because I want to get away from using the scripts in CFIDE. I am using plain ol' FORM, INPUT, and SELECT fields.

Here is the SELECT menu I am using, which does not display the correct selected value when I edit a record. What am I missing?

I think I might need to set up a CFIF statement -- if getProjectDetails.state has a value, then set that value as SELECTED ... but I am not sure what that looks like. =)

Thank you as always for your help.

Eric

Query:

<!--- get data from table #REQUEST.ProjectsTable# and convert the data into form variables --->
			  <cfquery name="getProjectDetails" datasource="#application.datasource#">
			    SELECT projectID
,ProjectName
,OSMVISTA
,Supervisor
,Address
,City
,State
,ZIP
,County
,Telephone
,Email
,Website
,CongressionalDistrict
,ProjectMission
,ProjectDescription
,DateCreated
,DateModified
,ParentID
,SortOrder
,projectKeywords
,projectDescriptionMeta
,projectOwner
,projectAuthor
,projectTitletag
,Archive
,Photo144
,Photo288
,Photo400
,Photo640
,Photo800
,projectPhoto
,leaderphoto

                FROM #REQUEST.ProjectsTable#
                WHERE projectID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.projectID)#">
 			  </cfquery>

  			<cfloop index="aCol" list="#getProjectDetails.columnList#">
			       <cfset "form.#aCol#" = getProjectDetails[aCol][getProjectDetails.currentRow]>
			  </cfloop>

Open in new window



SELECT Menu:
  <label for="State"><h3>State:</h3></label>
   <select size="10"
    name="State"
    value="State"
    multiple="no"
	tabindex="6"
    required="yes">
<option value="" <cfoutput>selected="#getProjectDetails.State#"</cfoutput>>Select a State</option> 
<option value="AL">Alabama</option> 
<option value="AK">Alaska</option> 
<option value="AZ">Arizona</option> 
<option value="AR">Arkansas</option> 
<option value="CA">California</option> 
<option value="CO">Colorado</option> 
<option value="CT">Connecticut</option> 
<option value="DE">Delaware</option> 
<option value="DC">District Of Columbia</option> 
<option value="FL">Florida</option> 
<option value="GA">Georgia</option> 
<option value="HI">Hawaii</option> 
<option value="ID">Idaho</option> 
<option value="IL">Illinois</option> 
<option value="IN">Indiana</option> 
<option value="IA">Iowa</option> 
<option value="KS">Kansas</option> 
<option value="KY">Kentucky</option> 
<option value="LA">Louisiana</option> 
<option value="ME">Maine</option> 
<option value="MD">Maryland</option> 
<option value="MA">Massachusetts</option> 
<option value="MI">Michigan</option> 
<option value="MN">Minnesota</option> 
<option value="MS">Mississippi</option> 
<option value="MO">Missouri</option> 
<option value="MT">Montana</option> 
<option value="NE">Nebraska</option> 
<option value="NV">Nevada</option> 
<option value="NH">New Hampshire</option> 
<option value="NJ">New Jersey</option> 
<option value="NM">New Mexico</option> 
<option value="NY">New York</option> 
<option value="NC">North Carolina</option> 
<option value="ND">North Dakota</option> 
<option value="OH">Ohio</option> 
<option value="OK">Oklahoma</option> 
<option value="OR">Oregon</option> 
<option value="PA">Pennsylvania</option> 
<option value="RI">Rhode Island</option> 
<option value="SC">South Carolina</option> 
<option value="SD">South Dakota</option> 
<option value="TN">Tennessee</option> 
<option value="TX">Texas</option> 
<option value="UT">Utah</option> 
<option value="VT">Vermont</option> 
<option value="VA">Virginia</option> 
<option value="WA">Washington</option> 
<option value="WV">West Virginia</option> 
<option value="WI">Wisconsin</option> 
<option value="WY">Wyoming</option>
</select>

Open in new window

0
Comment
Question by:Eric Bourland
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
Yep, you need a cfif. Check the option value. If it matches, mark the option as selected:

<option value="AL"
       <cfif getProjectDetails.State EQ "AL">selected</cfif>  >Alabama</option>

Of course you'd need to do that for *each* state <option>. A lot of changes if the list is really hard coded.  Hopefully you're storing the states in a db table, and populating the list with a query :) Then it's just:

(Watch the line wrapping...)

       <cfquery name="getStates">
        SELECT TheStateCode, TheStateTitle FROM ....
       </cfquery>

       <select name="state" ...>
           ...
        <cfoutput query="getStates">
              <option value="#getStates.TheStateCode#"
       <cfif getProjectDetails.State EQ getStates.TheStateCode>selected</cfif>>#getStates.TheStateTitle</option>
        </cfoutput>
        ....
      </select>
0
 
LVL 3

Author Comment

by:Eric Bourland
Comment Utility
Makes sense. I've got all of the states in a table. I will try it out! brb
0
 
LVL 3

Author Closing Comment

by:Eric Bourland
Comment Utility
Worked perfectly. _agx_, thank you as always. I'll try not to bug you again this weekend. =) Have a great weekend. Eric


    <!--- this query selects State Titles from tbl_NBPTS_Lookup_State --->
<cfquery datasource="#APPLICATION.dataSource#" name="GetStates">
        SELECT StateName
        FROM tbl_NBPTS_Lookup_State
</cfquery>

<li>
  <label for="State"><h3>State:</h3></label>
   <select size="10"
    name="State"
    multiple="no"
	tabindex="6"
    required="yes">
   <cfoutput query="GetStates">
    <option value="#GetStates.StateName#"
	<cfif getProjectDetails.State EQ GetStates.StateName>selected</cfif>>#GetStates.StateName#</option>
    </cfoutput>
</select>


             <span class="form_hint">Select state.</span>           
</li> 

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Always a pleasure.  Hope you enjoy the weekend!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

11 Experts available now in Live!

Get 1:1 Help Now