Solved

trouble with SELECT field

Posted on 2014-04-25
4
256 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
ID: 40023628
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
ID: 40023636
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
ID: 40023664
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_
ID: 40023752
Always a pleasure.  Hope you enjoy the weekend!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
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 …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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