选择选项

时间:2016-05-17 13:00:48

标签: coldfusion

我的ColdFusion代码出现问题。我正在尝试使用cfloop创建一个简单的HTML选择表单来填充选项。它可以工作,当你选择一个选项时,它会从数据库中提取该选项。但是一旦你这样做,唯一可用的选项是之前选择的选项。我做错了什么?

    <!--- Query the DataBase --->
<cfparam name="url.colors" default="">
<cfif structKeyExists(form, "colordb")>
    <cfset url.colordb = form.colordb>
</cfif>
<cfquery datasource="bentest" name="colors">
    SELECT *
    FROM color_codes
    <cfif structKeyExists(url,"colordb") and isNumeric(url.colordb)>
        WHERE id=#url.colordb#
    </cfif>
</cfquery>
<!--- Add Selector for user to select a color --->
<div class="selector">
    <cfoutput>
        <form action="?contentId=colorPickdb" name="clr" method="post" class="clr">
            <select class="clr" name="colordb" onChange="submit();">
                <option selected>Select A Color!</option>
                <cfloop query="colors">
                    <option value="#colors.id#">#colors.color#</option>
                </cfloop>   
            </select>
        </form>
    </cfoutput>
</div>
<div class="dump">
    <!--- Output results of Query --->
    <p><cfif structKeyExists(form, "colordb")>
        <cfoutput query="colors">
            #colors.color# <br>
            #colors.hexvalue# <br><br>
        </cfoutput>
        </cfif>
        <br>
    </p>
</div>

1 个答案:

答案 0 :(得分:1)

您可以在此处使用查询查询。

<!--- Query the DataBase --->
<cfparam name="url.colors" default="">
<cfquery datasource="bentest" name="colors">
    SELECT *
    FROM color_codes
</cfquery>
<!--- Add Selector for user to select a color --->
<div class="selector">
    <cfoutput>
        <form action="?contentId=colorPickdb" name="clr" method="post" class="clr">
            <select class="clr" name="colordb" onChange="submit();">
                <option selected>Select A Color!</option>
                <cfloop query="colors">
                    <option value="#colors.id#">#colors.color#</option>
                </cfloop>   
            </select>
        </form>
    </cfoutput>
</div>
<div class="dump">
    <!--- Output results of Query --->
    <p>
        <cfif structKeyExists(form, "colordb")>
            <cfquery dbtype="query" name="colorSelected">
                SELECT *
                FROM colors
                WHERE id=#form.colordb#
            </cfquery>
            <cfoutput query="colorSelected">
                #colorSelected.color# <br>
                #colorSelected.hexvalue# <br><br>
            </cfoutput>
        </cfif>
        <br>
    </p>
</div>