Coldfusion下拉查询

时间:2014-04-15 18:13:26

标签: jquery sql coldfusion coldfusion-9

因此需要根据用户选择的内容进行查询。学校,EID,校长和书籍的下拉菜单被列为选项。当"学校"选择或单击它将在sql-server数据库上执行查询并输入另一个下拉列表,供用户选择学校的所有值。我的英语不是很好,所以这就是我的意思

下拉菜单 - >如果选择学校 - > kogod,SIS,人文研究,Whartons商学院等(来自数据库)将填充。 - >如果单击沃顿商学院,它会显示该表。所以它就像是基于所选择的级联下拉列表。

那你怎么做的?代码请给我代码。感谢

<cfform method="POST" action=""> <select name="one" required="yes" onchange="this.form.submit()">
<option>Select Type</option>
  <option value="school">School</option>
  <option value="EID">Electronic ID</option>
  <option value="prinical">Principal's Name</option>
  <option value="books">Books Used</option>
</select>
</cfform>

<cfquery name="schoollist" datasource="xxyyzz" dbtype="ODBC">
          select school_title, 
                      from DATABASENAME order by school_title
        </cfquery>

<cfif one.school is "selected?" from the first DROPDOWN then another drown down will pop up with the list of schools >

帮助......请

1 个答案:

答案 0 :(得分:0)

我写了一个快速的解决方案,你应该可以很容易地扩展到几个层次。目前它只处理第一层,您从第一个下拉列表中选择一个值,它会向您显示下一个值,但您可以多次应用相同的概念。

<cfif structKeyExists(form, "one") and form.one neq "">
    <cfswitch expression="#form.one#">
        <cfcase value="school">
            <cfquery name="schoollist" datasource="xxyyzz" dbtype="ODBC">
              select school_title, 
              from DATABASENAME order by school_title
            </cfquery>

            <select name="schoollist" required="yes" onchange="this.form.submit()">
                <cfoutput query="schoollist">
                    <option value="#schoollist.school_title#">#schoollist.school_title#</option>
                </cfoutput>
            </select>
        </cfcase>

        <cfcase value="EID">
            <cfquery name="eidlist" datasource="xxyyzz" dbtype="ODBC">
              select eid, 
              from DATABASENAME order by eid
            </cfquery>

            <select name="eidlist" required="yes" onchange="this.form.submit()">
                <cfoutput query="eidlist">
                    <option value="#eidlist.eid#">#eidlist.eid#</option>
                </cfoutput>
            </select>
        </cfcase>

        <cfcase value="principle">
            <cfquery name="principle" datasource="xxyyzz" dbtype="ODBC">
              select principle, 
              from DATABASENAME order by principle
            </cfquery>

            <select name="principleList" required="yes" onchange="this.form.submit()">
                <cfoutput query="principle">
                    <option value="#principle.principle#">#principle.principle#</option>
                </cfoutput>
            </select>
        </cfcase>

        <cfcase value="books">
            <cfquery name="books" datasource="xxyyzz" dbtype="ODBC">
              select books, 
              from DATABASENAME order by books
            </cfquery>

            <select name="books" required="yes" onchange="this.form.submit()">
                <cfoutput query="books">
                    <option value="#books.books#">#books.books#</option>
                </cfoutput>
            </select>
        </cfcase>
    </cfswitch>
<cfelse>
    <cfform method="POST" action="#cgi.script_name#"> 
        <select name="one" required="yes" onchange="this.form.submit()">
          <option>Select Type</option>
          <option value="school">School</option>
          <option value="EID">Electronic ID</option>
          <option value="prinical">Principal's Name</option>
          <option value="books">Books Used</option>
        </select>
    </cfform>
</cfif>

您也可以通过执行此类操作来简化代码,但我目前无法对此进行测试,因此您可能必须使用它来使其工作:

    <cfif structKeyExists(form, "dropdown") and form.dropdown neq "">
    <!--- prevent sql injection, also serves to convert text in the database to valid table and column names for the next query--->
    <cfset sanitizedString = rereplacenocase(form.dropdown,'[^a-z_]','','all')>
    <!--- get data for drop down based on user selection --->
    <cfif structKeyExists(form, "rootType")>
        <cfset tableName = rereplacenocase(form.rootType,'[^a-z_]','','all')>
        <cfquery name="getInfo" datasource="mysql">
            SELECT * 
            FROM #tableName#
            WHERE #tableName# = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#form.dropdown#">
        </cfquery>

        <cfif getInfo.recordCount gt 0>
            <cfdump var="#getInfo#">
        <cfelse>
            No records were found.
        </cfif>
    <cfelse>
        <cfquery name="getDropDownData" datasource="mysql">
          select #sanitizedString#
          from #sanitizedString# order by <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#form.dropdown#">
        </cfquery>

        <cfform method="POST" action="#cgi.script_name#">
            <input type="hidden" name="rootType" value="<cfoutput>#form.dropdown#</cfoutput>">
            <select name="dropdown" required="yes" onchange="this.form.submit()">
                        <option value="">Select Value</option>
                <cfoutput query="getDropDownData">
                    <cfset val = getDropDownData["#sanitizedString#"]>
                    <option value='#val#'>#val#</option>
                </cfoutput>
            </select>
        </cfform>
    </cfif>
    <!--- redisplay the form --->

<cfelse>
    <cfform method="POST" action="#cgi.script_name#"> 
        <select name="dropdown" required="yes" onchange="this.form.submit()">
          <option>Select Type</option>
          <option value="school">School</option>
          <option value="EID">Electronic ID</option>
          <option value="principal">Principal's Name</option>
          <option value="books">Books Used</option>
        </select>
    </cfform>
</cfif>