Warm tip: This article is reproduced from serverfault.com, please click

html-使用下拉菜单并引用查询结果

(html - Using dropdown menu and referencing query results)

发布于 2020-12-31 16:28:02

我希望这是一个简单的问题,我只是不知道如何在谷歌上搜索正确的答案。

我有一个使用查询填充的下拉列表。

<cfquery name="getFruits" datasource="#application.dsnName#" username="#application.dsnUser#" password="#application.dsnPass#">
SELECT TOP 3 fruit, color, size FROM fruit_table
</cfquery>

<cfselect name="fruits" query="getFruits" display="fruit" value="fruit" selected="#form.fruit#" queryPosition="below" required="yes" >
<option value="">Select Fruit</option>
</cfselect>

当用户从下拉列表中选择“水果”以引用与他们在另一个查询中选择的“水果”相关联的“大小”和“颜色”时,是否有可能?

例如:

<cfquery name="getFruits" datasource="#application.dsnName#" username="#application.dsnUser#" password="#application.dsnPass#">
SELECT vegetable FROM vegetable_table WHERE size = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="50" value="#form.size#"> AND color = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="50" value="#form.color#">
</cfquery>

谢谢!

Questioner
AShoes
Viewed
0
user12031119 2021-01-06 00:28:55

当你的表单被提交时,该值form.fruits将在没有colorsize属性的情况下发布因此,你重新编码<cfquery>(我其重命名为“getVegetables”)以从所选水果中获取这些属性的方式将是这样的。

<cfquery name="getVegetables" datasource="#application.dsnName#" username="#application.dsnUser#" password="#application.dsnPass#">
    SELECT
        vegetable 
    FROM 
        vegetable_table 
    WHERE (size, color) IN
        (SELECT
            size, color 
        FROM 
            fruit_table
        WHERE
            fruit = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="50" value="#form.fruits#">)
</cfquery>

顺便说一句,我不喜欢使用<cfselect>或任何<cfform>标签,但这不是你的问题。但是,我强烈建议你丢弃它并重构你的代码。

编辑(第二次尝试):

作为替代答案,如果你想传递size,color用逗号连接并别名为sizeColor表单的fruit而不是来自你<cfquery>,那么你可以做的是将第一个 select 语句更改为

<cfquery name="getFruits" datasource="#application.dsnName#" username="#application.dsnUser#" password="#application.dsnPass#">
SELECT TOP 3 
fruit,
size || ',' || color AS sizeColor 
FROM fruit_table
</cfquery>

然后将 value 属性更改为value="sizeColor"in your<cfselect>这将是上述逗号分隔的大小和颜色列表。所以代码将改为

<cfselect name="fruits" query="getFruits" display="fruit" value="sizeColor" selected="#form.fruit#" queryPosition="below" required="yes" >
<option value="">Select Fruit</option>
</cfselect>

然后你可以<cfquery>将发布页面上的更改为这个。

<cfquery name="getVegetables" datasource="#application.dsnName#" username="#application.dsnUser#" password="#application.dsnPass#">
SELECT vegetable FROM vegetable_table 
WHERE 
    size = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="50" value="#form.fruits.listGetAt(1)#"> AND 
    color = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="50" value="#form.fruits.listGetAt(2)#">
</cfquery>