Coldfusion 会话变量

发布于 2024-11-25 18:31:54 字数 338 浏览 2 评论 0原文

我们有一个由查询动态填充的列表框。当我们在列表框中选择一个值并提交表单时,结果将显示在同一页面中。现在我们希望在以下情况下保留在列表框中选择的值:表格已提交。我们该如何处理?我们尝试使用以下代码在会话中设置变量: 会话:#session.sPcwQua#

现在我们如何使用列表框中设置的会话变量(在本例中为 session.sPcwQua),以便保留列表框中选定的值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2024-12-02 18:31:54

您可能想使用表单范围而不是会话范围:
设置表单默认值:

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

运行查询:

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

填充选择框:

<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>

[可能需要检查其中的一些语法 - 因为我没有;t ;)]

-sean

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

深府石板幽径 2024-12-02 18:31:54

如果您有特殊需要“保留值”并且想要设置 session.sPcwQua 变量,您可以尝试以下操作:

<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>

如果您只是尝试提交表单并返回元素值,那么您应该按照 Seasn 所说的操作:

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文