Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remembering a dynamic list choice

Status
Not open for further replies.

RikForgo

Programmer
Jul 31, 2002
59
US
I've been working with one of Ben Forta's examples and I'd like to modify it. In it's current form, the code (below) uses session variables to remember the last keyword that was queried. The action page and the result page are the same in this instance (i.e., the search is done and the user is returned to the same page, and the results appear below the search entry box).

But now I need to modify the CFFORM code so that rather than the user typing in a keyword, he simply chooses from a select-driven list. The dynmic list driven portion of the code I have, but I'm confused about how to incorporate the session variables needed to remember the selection.

Any help is appreciated.

-Rik

----------------------------------------------
Original (works)

<!--- When user submits form, save search criteria in Client variable --->
<CFIF IsDefined(&quot;Form.SearchCriteria&quot;)>
<CFSET CLIENT.LastSearch = Form.SearchCriteria>
<!--- If not submitting yet, get prior search word (if possible) --->
<CFELSEIF IsDefined(&quot;CLIENT.LastSearch&quot;)>
<CFSET SearchCriteria = CLIENT.LastSearch>
<!--- If no prior search criteria exists, just show empty string --->
<CFELSE>
<CFSET SearchCriteria = &quot;&quot;>
</CFIF>

<CFSET PageStatus = &quot;Search&quot;>

<CFFORM ACTION=&quot;#CGI.SCRIPT_NAME#&quot; METHOD=&quot;Post&quot; class=&quot;maintext&quot;>
<table width=&quot;100%&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr>
<td width=&quot;28%&quot;>
<!--- &quot;Search Criteria&quot; field --->
<span class=&quot;maintext2bold&quot;>Search</span><p>
<span class=&quot;maintext2&quot;>Enter keyword or phrase:<br>
</span>
<cfinput name=&quot;SearchCriteria&quot; value=&quot;#SearchCriteria#&quot;
required=&quot;Yes&quot;
message=&quot;Your query is blank - please enter a word or phrase.&quot; class=&quot;maintext2&quot;>
</td>
<td width=&quot;34%&quot; align=&quot;right&quot; valign=&quot;bottom&quot;>
<!--- &quot;Submit&quot; button --->
<input name=&quot;Submit&quot; type=&quot;Submit&quot; class=&quot;button&quot; value=&quot;Search&quot;></td>
</tr>
<tr>
<td colspan=&quot;4&quot;><hr noshade></td>
</tr>
</table>
</CFFORM>

<!--- If we have something to search for, do it now --->

<CFIF SearchCriteria NEQ &quot;&quot;>

-------------------------------------------------
Modified to include a dynamic list (a mess)

<cfparam name=&quot;wsh_year&quot; default=&quot;WSH_YEAR&quot;>
<cfparam name=&quot;id&quot; default=&quot;id&quot;>

<CFQUERY NAME=&quot;get_ws&quot; DATASOURCE=&quot;attro&quot;
CACHEDWITHIN=&quot;#CreateTimeSpan(0,0,15,0)#&quot;>
SELECT system, wsh_year, id
FROM wsh
WHERE wsh_year = '2002'
ORDER BY system, wsh_year
</CFQUERY>


<CFQUERY NAME=&quot;GetWS&quot; DATASOURCE=&quot;attro&quot;>
SELECT id
FROM wsh
</CFQUERY>

<!--- When user submits form, save search criteria in Client variable --->
<CFIF IsDefined(&quot;Form.SearchCriteria&quot;)>
<CFSET CLIENT.LastSearch = Form.SearchCriteria>
<!--- If not submitting yet, get prior search word (if possible) --->
<CFELSEIF IsDefined(&quot;CLIENT.LastSearch&quot;)>
<CFSET SearchCriteria = CLIENT.LastSearch>
<!--- If no prior search criteria exists, just show empty string --->
<CFELSE>
<CFSET SearchCriteria = &quot;&quot;>
</CFIF>

<CFSET PageStatus = &quot;Search&quot;>

<CFFORM ACTION=&quot;#CGI.SCRIPT_NAME#&quot; METHOD=&quot;Post&quot; class=&quot;maintext&quot;>
<table width=&quot;100%&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr>
<td width=&quot;28%&quot;>
<!--- &quot;Search Criteria&quot; field --->
<span class=&quot;maintext2bold&quot;>Search</span>
<p> <span class=&quot;maintext2&quot;>Select a system to cross reference:<br>
</span>
<cfinput name=&quot;SearchCriteria&quot; class=&quot;dropdown&quot;> <cfoutput query=&quot;get_ws&quot;
option value=&quot;#get_ws.system#&quot;
#get_ws.system# </CFOUTPUT>
required=&quot;Yes&quot;
message=&quot;Your query is blank - please enter a word or phrase.&quot;>
</td>
<td width=&quot;34%&quot; align=&quot;right&quot; valign=&quot;bottom&quot;>
<!--- &quot;Submit&quot; button --->
<input name=&quot;Submit&quot; type=&quot;Submit&quot; class=&quot;button&quot; value=&quot;Search&quot;></td>
</tr>
<tr>
<td colspan=&quot;4&quot;><hr noshade></td>
</tr>
</table>
</CFFORM>

<!--- If we have something to search for, do it now --->

<CFIF SearchCriteria NEQ &quot;&quot;>
 
You wouldn't use CFINPUT, but rather SELECT, or CFSELECT, when building your dynamic list.
Code:
<p>
<span class=&quot;maintext2&quot;>Select a system to cross reference:<br></span> 
<cfselect name=&quot;SearchCriteria&quot; query=&quot;get_ws&quot; value=&quot;system&quot; display=&quot;system&quot; message=&quot;Your query is blank - please enter a word or phrase.&quot;><option value=&quot;&quot;>Select a system</option></cfselect>
</p>
[code]
I can't remember if &quot;required&quot; actually works if the size is 1 (ie- if it's a true dropdown box). Seems to me I recall that it doesn't (I never use CFSELECT myself)... you have to set the size to 2 or greater for required to work (otherwise there's [i]always[/i] something selected).

As an alternative, just use a standard SELECT:
[code]
<SELECT name=&quot;SearchCriteria&quot;>
<OPTION VALUE=&quot;&quot;>Select one</OPTION>
<CFOUTPUT query=&quot;get_ws&quot;>
<OPTION VALUE=&quot;#get_ws.system#&quot;>#get_ws.system#</OPTION>
</CFOUTPUT>
</SELECT>
[code]

Either way... since you're using METHOD=POST on your form, the value that the user selected in the dropdown will be passed to your action page in the #FORM.SearchCriteria# variable (or whatever you name the select tag), just like in Ben's original code.

Hope it helps,
 
-Carl
 
Thanks Carl. It works, but when it returns the page (the action/results page are the same) the drop list goes directly to the first item in the list. If the user selected &quot;Zurich&quot; how can I get the list to display that rather than &quot;Amsterdam,&quot; which is the first item on the list? In Ben's example the 'SearchCriteria' variable was remembered ... somehow that's getting lost in the drop list translation.
 
Ahhh... again... two possible ways. If you're using CFSELECT you need to add the &quot;selected&quot; parameter:
Code:
<cfparam name=&quot;FORM.SearchCriteria&quot; default=&quot;&quot;>

<cfselect name=&quot;SearchCriteria&quot; query=&quot;get_ws&quot; value=&quot;system&quot; display=&quot;system&quot;
Code:
selected=&quot;#FORM.SearchCriteria#&quot;
Code:
 message=&quot;Your query is blank - please enter a word or phrase.&quot;><option value=&quot;&quot;>Select a system</option></cfselect>

or, if you're just using select:
Code:
<cfparam name=&quot;FORM.SearchCriteria&quot; default=&quot;&quot;>

<SELECT name=&quot;SearchCriteria&quot;>
<OPTION VALUE=&quot;&quot;>Select one</OPTION>
<CFOUTPUT query=&quot;get_ws&quot;>
<CFIF CompareNoCase(get_ws.system,FORM.SearchCriteria) EQ 0>
   <OPTION VALUE=&quot;#get_ws.system#&quot; SELECTED=&quot;selected&quot;>#get_ws.system#</OPTION>
<CFELSE>
   <OPTION VALUE=&quot;#get_ws.system#&quot;>#get_ws.system#</OPTION>
</CFIF>
</CFOUTPUT>
</SELECT>

Like I said, I haven't looked at Ben's code... so I don't know why you'd need to use a session variable to store the selected item. It's usually not necessary since, as above, the value already being passed in the FORM structure. But I'm certainly not going to be the one to argue with Forta! ;-) If you really need to store it in a session variable, let me know (and let me know why) and I'm sure we can figure it out.

Does that do it for you?
-Carl
 
Yes, that got me where I needed to go. Thanks!

I still have one minor dilemma with it, though. The dynamic list does everything I need it to do, except pass the ID of the specific record. The &quot;FORM.SearchCriteria&quot; parameter is passed successfully, but beneath all that muck I have two queries that will try to search two other tables for related records based on the ID of the choice they made in the dynamic list. However, only the name of the choice is passed (e.g., &quot;Zurich&quot;). If Zurich has a record ID of '45', how do I pass that along as a referenceable variable/parameter?

This is the additoional query I'm using:

<CFQUERY DATASOURCE=&quot;attro&quot; name=&quot;q_city_search&quot;>
select w.city, w.acronym, t.project_name, t.tid_year, t.tids_id
from wsh w, ie_wsh_tid i, tids t
where w.id = #URL.id#
and w.id = i.wsh_id
and i.tids_id = t.tids_id;
</cfquery>
 
You would set up the &quot;display&quot; and the &quot;value&quot; parameters as different data. &quot;display&quot; would be the column you want displayed in the dropdown (to the user), &quot;value&quot; would be the column you want to actually be posted based on the choice they made:

Code:
<cfselect name=&quot;SearchCriteria&quot; query=&quot;get_ws&quot;
Code:
value=&quot;id&quot;
Code:
 display=&quot;system&quot; selected=&quot;#FORM.SearchCriteria#&quot; message=&quot;Your query is blank - please enter a word or phrase.&quot;><option value=&quot;&quot;>Select a system</option></cfselect>

Now... even though the items in the dropdown will be the rows of &quot;system&quot;, #FORM.SearchCriteria# will contain the id of the selected item.

Basically it's the same as writting a standard HTML select box that looks like:
Code:
<select name=&quot;SearchCriteria&quot;>
<option value=&quot;&quot;>Select a system</option>
<option value=&quot;1234&quot;>Item 1</option>
<option value=&quot;5678&quot;>Item 2</option>
<option value=&quot;4444&quot;>Item 3</option>
<option value=&quot;0987&quot;>Item 4</option>
</select>

If the user chose &quot;Item 2&quot; from the above dropdown and submitted the form, #FORM.SearchCriteria# would contain &quot;5678&quot;.

-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top