Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Save selection of a drop down box in a variable

I have a drop down list box that get populated from a database.  I want to save the #idumodel# field to a variable.  If the users changes there selection I also need it to update the variable with the new number.  Below is the code I use to populate the list box.

                                     <label>
                                     <select name="lmodel" required="yes">
                                     <option>Select Model</option>
                                     <cfoutput query="qmodel">
                                     <option value="#idumodel#">#model#</option>
                                     </cfoutput>
                                     </select>
                                     </label>
0
sonicimpulse
Asked:
sonicimpulse
  • 12
  • 9
1 Solution
 
SidFishesCommented:
to do that you need to reload the page to set the variable after the select onChange event happens

if you have other values on the page that could exist you would have to set url string variables for them as well

You could also do this using ajax but it's a bit more involved

<cfparam name="url.idumodel" default="">
 
<cfif url.idumodel neq "">
<cfset session.idumodel = url.idumodel>
</cfif> 
 
<label>
<select name="lmodel" required="yes"  onChange="window.open(this.options[this.selectedIndex].value,'_self')">
<option>Select Manufacturer</option>
<cfoutput query="qmodel">
thispage.cfm?idumodel=#qmodel.idumodel#.html" 
<cfif qmodel.idumodel. eq url.idumodel>selected</cfif>>#model#
<option value="#idumodel#">#model#</option>
</cfoutput>
</select>
</label>

Open in new window

0
 
SidFishesCommented:
oops sorry paste error


<cfparam name="url.idumodel" default="">
 
<cfif url.idumodel neq "">
<cfset session.idumodel = url.idumodel>
</cfif> 
 
<label>
<select name="lmodel" required="yes"  onChange="window.open(this.options[this.selectedIndex].value,'_self')">
<option>Select Manufacturer</option>
<cfoutput query="qmodel">
 
 
<option value="thispage.cfm?idumodel=#qmodel.idumodel#.html"
		<cfif qmodel.idumodel. eq url.idumodel>selected</cfif>>
		#model#
</option>
</cfoutput>
</select>
</label>

Open in new window

0
 
sonicimpulseAuthor Commented:
I'm assuming that I'm supposed to setup my query on the second page as follows

<cfquery name="listallused" datasource="users">
SELECT *
FROM vehicles
WHERE  model="#url.idumodel#">
</cfquery>

If this is correct its not working.  Can you please tell me the correct way
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
SidFishesCommented:
you didn't say you were doing a db update... or moving to a second page

what the code does is reload a single page and set a variable (or update it)

however, since session variables persists over pages, you can use the variable

session.idumodel on subsequent pages

ie:

WHERE  model="#session.idumodel#">

0
 
sonicimpulseAuthor Commented:
Here is the error I'm getting now.

Element IDUMODEL is undefined in SESSION.  
 
 
The error occurred in C:\ColdFusion8\wwwroot\automotive\upricelisting.cfm: line 43
 
41 : SELECT *
42 : FROM vehicles
43 : WHERE  model="#session.idumodel#">
44 : </cfquery>
45 :
 
0
 
incapitalCommented:
It looks like you have a superfluous > in your query.
0
 
sonicimpulseAuthor Commented:
I fixed that.  I even tried getting rid of the query and just doing a cfoutput of the variable to see if there was anything there and I still get the same error.
0
 
SidFishesCommented:
do you have sessionmanagement turned on in cfapplication?

<cfapplication  sessiontimeout="20" name="YourApp" sessionManagement="true">
0
 
sonicimpulseAuthor Commented:
I created a file called application.cfm and put the following line of code in it

<cfapplication  sessiontimeout="20" name="YourApp" sessionManagement="true">

I still get the same error
0
 
SidFishesCommented:
the code i posted works.. I use it all the time... since i don't know what you are doing with it before or after ..without more code it's impossible to diagnose...
0
 
sonicimpulseAuthor Commented:
I'm using this code that you gave me above on the first page.

The second page only has <cfoutput>#session.idumodel#</cfoutput>
<cfparam name="url.idumodel" default="">
 
<cfif url.idumodel neq "">
<cfset session.idumodel = url.idumodel>
</cfif> 
 
<label>
<select name="lmodel" required="yes"  onChange="window.open(this.options[this.selectedIndex].value,'_self')">
<option>Select Manufacturer</option>
<cfoutput query="qmodel">
 
 
<option value="thispage.cfm?idumodel=#qmodel.idumodel#.html"
                <cfif qmodel.idumodel. eq url.idumodel>selected</cfif>>
                #model#
</option>
</cfoutput>
</select>
</label>

Open in new window

0
 
sonicimpulseAuthor Commented:
Is the location of the cfset in the wrong place.  It would need to change every time the user clicks on the drop down box
0
 
SidFishesCommented:
it's in the right place

add this to the page

<cfdump var="#session#">

and post the results

also..how are you getting to the second page
0
 
sonicimpulseAuthor Commented:
I put that cfdump on both pages and here is the results

cfid         6301
cftoken       60069496
sessionid       YOURAPP_6301_60069496
urltoken       CFID=6301&CFTOKEN=600

Here is how i'm getting to the second page.  I took your code from above and replaced the "thispage.cfm" to the correct page "umodellisting.cfm"

0
 
SidFishesCommented:
ok well that's the problem...

you have to replace thispage.cfm with the name of the page that the code is on...otherwise doesn't reload and the variable never gets set..

0
 
sonicimpulseAuthor Commented:
ok couple of things and this is almost working the way I want.  If you want me to start a new help for this question just say so.  

When the pages reloads to set the session variable is clears the form.  I would like what ever the user picks in the drop down boxes to stay.

The second issue is when it saves the session variable it saves it as 60.htm or what ever the correct number is.
0
 
SidFishesCommented:
actually both issues should be fixed by fixing this typo
                                                                                                 ***
<option value="thispage.cfm?idumodel=#qmodel.idumodel#.html" <cfif qmodel.idumodel eq url.idumodel>selected</cfif>>

should be

<option value="thispage.cfm?idumodel=#qmodel.idumodel#"
                <cfif qmodel.idumodel eq url.idumodel>selected</cfif>>
0
 
sonicimpulseAuthor Commented:
Ok this is my code below.  Its saving the variable correctly but whats happening is its not displaying the values the user chose in the drop down boxes.  Its going back to the default.  This is all on the first page not the second page that the link goes to.  
0
 
sonicimpulseAuthor Commented:
I also noticed one other issue.  Its updating the variable but its always one behind.  So lets say my second selection was a honda accord and my third selection was a audi a4.  The variable should read audi a4 id but its reading the honda accord id.
0
 
sonicimpulseAuthor Commented:
All I'm trying to do is get these drop down boxes to work and store the selection of the second box in a variable.  Can someone please help me with this.
0
 
SidFishesCommented:
the code I provided does exactly what you asked ... you didn't mention multiple selects. You can't expect us to know what you want to do if you don't post complete code and/or let us know from the outset that the code you post is not all you want.

0
 
sonicimpulseAuthor Commented:
Thanks sorry for the misconfusion
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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