简体   繁体   中英

Coldfusion session variables

We have a list box which is getting dynamically populated by a query.When we are selecting a value in the list box and submitting the form the results are displayed in the same page.Now we want to retain the value selected in the list box when the form is submitted.How do we go about this? We have tried to set the variable in a session with the below code: Session:#session.sPcwQua#

Now how do we use the session variable which is set that is in our case session.sPcwQua in the list box so that the selected value in the list box is retained.

If You have special need to "retain value" and you want to set the session.sPcwQua variable you can try this:

<cfif isDefined("form.sPcwQua")><cfset session.sPcwQua = form.sPcwQua></cfif>

<cfquery name="getsPcwQua" datasource="yourdsn">
    select x from tablex
</cfquery>

<form name="someform" action="">

    <select name="sPcwQua">
        <cfloop query="getsPcwQua">
            <option value='#getsPcwQua.x#' <cfif getsPcwQua.x eq session.sPcwQua >selected</cfif> >#getsPcwQua.x#</option>
        </cfloop>
    </select>

</form>

If You just trying to submit form and return element value than you should do what Seasn says:

<cfset fsPcwQua="" />
<cfif isDefined("form.sPcwQua")><cfset fsPcwQua = form.sPcwQua></cfif>

<cfquery name="getsPcwQua" datasource="yourdsn">
    select x from tablex
</cfquery>

<form name="someform" action="">

    <select name="sPcwQua">
        <cfloop query="getsPcwQua">
            <option value='#getsPcwQua.x#' <cfif getsPcwQua.x eq fsPcwQua >selected</cfif> >#getsPcwQua.x#</option>
        </cfloop>
    </select>

</form>

You probably want to use the form scope rather than the session scope: Set your form defaults:

 <cfparam name="form.sPcwQua" default="" />

run your query:

 <cfquery name="getsPcwQua" datasource="...

populate your selectbox:

<select name="form.sPcwQua">
    <cfloop query="getsPcwQua">
    <cfif form.sPcwQua eq getsPcwQua.value>
          <cfset selected = 'selected="selected"' />
    <cfelse>
          <cfset selected = '' />
    </cfif>
    <option value='#getsPcwQua.value#' #selected#>#getsPcwQua.value#</option>
    </cfloop>
</select>

[might have to check some of the syntax in there - cause I didn;t;)]

-sean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM