Solved

Save selection of a drop down box in a variable

Posted on 2008-10-16
22
645 Views
Last Modified: 2010-04-21
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
Comment
Question by:sonicimpulse
  • 12
  • 9
22 Comments
 
LVL 36

Expert Comment

by:SidFishes
ID: 22740881
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
 
LVL 36

Accepted Solution

by:
SidFishes earned 500 total points
ID: 22740910
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
 

Author Comment

by:sonicimpulse
ID: 22744214
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22744557
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
 

Author Comment

by:sonicimpulse
ID: 22744694
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
 
LVL 3

Expert Comment

by:incapital
ID: 22744927
It looks like you have a superfluous > in your query.
0
 

Author Comment

by:sonicimpulse
ID: 22744942
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22745060
do you have sessionmanagement turned on in cfapplication?

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

Author Comment

by:sonicimpulse
ID: 22745106
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22745141
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
 

Author Comment

by:sonicimpulse
ID: 22745163
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:sonicimpulse
ID: 22745174
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22745223
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
 

Author Comment

by:sonicimpulse
ID: 22745303
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22745364
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
 

Author Comment

by:sonicimpulse
ID: 22745495
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22745566
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
 

Author Comment

by:sonicimpulse
ID: 22746443
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
 

Author Comment

by:sonicimpulse
ID: 22746468
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
 

Author Comment

by:sonicimpulse
ID: 22760510
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22761143
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
 

Author Closing Comment

by:sonicimpulse
ID: 31506998
Thanks sorry for the misconfusion
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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 …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 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

13 Experts available now in Live!

Get 1:1 Help Now