Solved

trouble with SELECT field

Posted on 2014-04-25
4
257 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 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