Skip to main content
December 23, 2008
Question

Get selected value of cfselect

  • December 23, 2008
  • 9 replies
  • 11369 views
Hi. What I want to do should be simple, but it's killing me! All I want to do is display the selected value of a cfselect on my page. Here's the code:

Select:
<cfselect name="ddlStates" value="fullname" display="fullname" width="200"
style="font-size:11px"
bind="cfc:cfc.nationwideJobs.getStates()"
bindonload="true">
</cfselect>

<cfset var1 = #form.ddlStates.selectedItem.label#>
<cfoutput>#var1#</cfoutput>

This doesn't work. The error I keep getting is:

Error Occurred While Processing Request
Element DDLSTATES.SELECTEDITEM.LABEL is undefined in FORM.

Thanks for the help.


This topic has been closed for replies.

9 replies

December 24, 2008
The page loads, but my cfgrid is empty. No matter which state I pick, I do not get results returned to the cfgrid. I am cfoutput'ing the selected value next to the submit button to be sure that I can select it. Also, as I stated earlier, the state I pick is passed to the EXEC statement in the SQL query, but the grid isn't updated. I can see, however that the number of records returned:

qryGetJobsByState (Datasource=ttcmdev_local, Time=32ms, Records=223) in C:\Inetpub\wwwroot\Demo\cfc\jobsByState.cfc @ 12:42:40.040

EXEC sp_GetJobsByState
@state='Maryland'

Any ideas? Is there something wrong with my code?
Inspiring
December 24, 2008
If it's not working, what is actually happening?
Inspiring
December 24, 2008
Your cfc knows nothing about forms. All it knows about are the arguments you pass to it.
December 24, 2008
Dan,

Thanks for your replies. So, what I need to do is pass the selected value from the cfselect as an argument to the cfc? Is there anything special to passing the argument to the cfc from the cfselect? Do I need to use hidden fields to hold the variable or anything like that?

Cheers,

Byron
December 24, 2008
I've decided to go about this in a different way. I would like to pass the cfselect's selected value to an action form that will display my filtered results. Here's the cfm for the base page:

<cfform action="viewFilteredJobs.cfm" method="post">
<select name="ddlStates">
<option value="Alabama" label="Alabama">
<option value="Alaska" label="Alaska">
<option value="Arizona" label="Arizona">
<option value="Colorado" label="Colorado">
<option value="Maryland" label="Maryland">
</select>
</p>
<input type="submit" name="Submit" value="Submit" />
</cfform>

Here's the cfm for the action page:

<cfform format="html" method="post" action="viewFilteredJobs.cfm">
<table border="0" align="center">
<tr><td><a href="viewNationWideJobs.cfm" style="font-size:11px;font-family:Arial">Go Back</a></td></tr>
<tr>
<td colspan="3">
<cfgrid name="grdJobsByState" selectmode="row" griddataalign="center" pagesize="30" format="html"
bind="cfc:cfc.jobsByState.getJobsByState({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection})">
<cfgridcolumn name="jobtitle" header="Job Title" headeralign="center" width="500" dataalign="left">
<cfgridcolumn name="certified" header="Cert/Lic Req?" headeralign="center" width="100" dataalign="center">
<cfgridcolumn name="searchstate" header="State" headeralign="center" width="50" dataalign="center">
<cfgridcolumn name="date" header="Date" headeralign="center" width="150" dataalign="center">
</cfgrid>
</td>
</tr>
<tr><td><cfinput type="hidden" name="selectedState" value="#form.ddlStates#">
</table>
</cfform>

Finally, here's the cfc:

<cfcomponent>
<cffunction name="getJobsByState" access="remote">
<cfargument name="page" required="no" default="1">
<cfargument name="pageSize" required="yes" default="20">
<cfargument name="gridsortcolumn" required="yes" default="date">
<cfargument name="gridsortdirection" required="yes" default="desc">

<cfif arguments.gridsortcolumn eq "">
<cfset arguments.gridsortcolumn = "date" />
<cfset arguments.gridsortdirection = "desc" />
</cfif>

<cfquery name="qryGetJobsByState" datasource="ttcmdev_local">
EXEC sp_GetJobsByState
@state='#form.ddlStates#'
</cfquery>
<!---<cfreturn qryGetJobsByState>--->
<cfreturn queryconvertforgrid(qryGetJobsByState, page, pagesize)>
</cffunction>
</cfcomponent>

No matter what I do, I keep getting this error:

Element DDLSTATES is undefined in FORM.
The error occurred in C:\Inetpub\wwwroot\Demo\cfc\jobsByState.cfc: line 15

13 : <cfquery name="qryGetJobsByState" datasource="ttcmdev_local">
14 : EXEC sp_GetJobsByState
15 : @state='#form.ddlStates#'
16 : </cfquery>
17 : <!---<cfreturn qryGetJobsByState>--->

What am I missing? Thanks for the help!

Inspiring
December 23, 2008
The grid thing might entail a form submission. In the meantime, here is some sample code that changes the contents of a div based on the choice of a select. It might give you some hints.

<cfsavecontent variable="PickUnits">
bunch of cf & html code goes here.
</cfsavecontent>


<cfsavecontent variable="PickServices">
bunch of cf & html code goes here.
</cfsavecontent>


<script>
function toggle (choice) {
<cfoutput>
var #toScript(PickUnits, "UnitsPicked")#;
var #toScript(PickServices, "ServicesPicked")#;
</cfoutput>

if (choice == "service")
document.getElementById('UnitsOrServices').innerHTML = ServicesPicked;

else
document.getElementById('UnitsOrServices').innerHTML = UnitsPicked;

return true;
} // end of function

</script>

<select name="ReportBy" onchange="toggle(this.value);">
<option value="unit">Unit</option>
<option value="service">Service</option>
</select>


<cfoutput>
<div id="UnitsOrServices">
#PickUnits#
</div>
</cfoutput>
December 23, 2008
Dan,

Thanks for the sample. This is a good start. I will try to get this working for my needs.

Cheers,

b
Inspiring
December 23, 2008
What you really want to do is bind the cfselect to the cfgrid.
I assume your using cf8

Then here is an example of this
http://www.garyrgilbert.com/blog/index.cfm/2007/11/20/Filtering-CFGrid-with-CFSelect

Ken
December 23, 2008
*Ideally*, what I'd like to do is change the contents of a cfgrid based on the selected value of a cfselect. I have a cfc that populates the cfselect with US States. The cfgrid should show values based on what is selected in the cfgrid.

For now, I'll accept getting the value out of the cfselect. Do I have to submit the form to get the cfselect's selected value? The cfselect is already rendered/populated by the time I see it in the browser, so what am I missing?
Inspiring
December 23, 2008
You appear to be attempting to display a form variable before submitting the form. Is that what you think you are trying to do?
Inspiring
December 23, 2008
byronfromwes92 wrote:
> This doesn't work. The error I keep getting is:
>

That's right it does not work. ColdFusion runs on the server and
JavaScript runs on the client and never shall the two meet.

The problem is that when CFML is running and trying to set var1 to
'from.ddlStates.selectedItem.label' nothing has been delivered to the
client and nobody has selected anything in the control yet.

By the time anything is delivered to the client for them to select
stuff, ColdFusion is completely done with the request and moved on to
somebody else's.

Any other words, that is a long way to say that you need to use
JavaScript to access the user interaction with the Select control once
it has been delivered to the client.


December 23, 2008
Thanks for the response. Will you please show a sample of how to do what you suggest?
Inspiring
December 23, 2008
Doesn't this work?

<cfset var1 = #form.ddlStates#>

--
Ken Ford
Adobe Community Expert Dreamweaver/ColdFusion
Adobe Certified Expert - Dreamweaver CS3
Adobe Certified Expert - ColdFusion 8
Fordwebs, LLC
http://www.fordwebs.com
http://www.cfnoob.com


"byronfromwes92" <webforumsuser@macromedia.com> wrote in message news:gir20i$bob$1@forums.macromedia.com...
> Hi. What I want to do should be simple, but it's killing me! All I want to do
> is display the selected value of a cfselect on my page. Here's the code:
>
> Select:
> <cfselect name="ddlStates" value="fullname" display="fullname"
> width="200"
> style="font-size:11px"
> bind="cfc:cfc.nationwideJobs.getStates()"
> bindonload="true">
> </cfselect>
>
> <cfset var1 = #form.ddlStates.selectedItem.label#>
> <cfoutput>#var1#</cfoutput>
>
> This doesn't work. The error I keep getting is:
>
> Error Occurred While Processing Request
> Element DDLSTATES.SELECTEDITEM.LABEL is undefined in FORM.
>
> Thanks for the help.
>
>
>
>
December 23, 2008
When I attempt to use <cfset var1 = #form.ddlStates#>, the error I get is: Element DDLSTATES is undefined in FORM.