Skip to main content
Inspiring
April 21, 2015
Answered

Form not filtering results

  • April 21, 2015
  • 1 reply
  • 661 views

I'm creating 2 filtering drop downs and I can't figure out why my code isn't filtering the results on the page.  Please help...

Here's the code.

<cfparam name="form.personnel" default="">

<cfparam name="form.status" default="">

<cfquery name="q" datasource="#dsn#">

     SELECT *

     FROM table

     WHERE 1=1

          <cfif val(form.personnel) GT 0>

               AND Sector = <cfqueryparam value="#form.personnel#" cfsqltype="cf_sql_varchar">

          </cfif>

          <cfif val(form.status) GT 0>

               AND reqStatus = <cfqueryparam value="#form.status#" cfsqltype="cf_sql_varchar">

          </cfif>

</cfquery>

<form method="post" action="">

     <select name="personnel">

               <option></option>

     </select>

     <select name="status">

               <option></option>

     </select>

     <input type="submit" />

</form>

<cfoutput query="q">

     #sector# || #reqstatus#

</cfoutput>

    This topic has been closed for replies.
    Correct answer haxtbh

    Why are you checking "<cfif val(form.personnel) GT 0>" in the SQL query? What are your reasons for using val()? Your results are most likely not being filtered as your SQL query is not working as you are checking your variables wrong.

    Are you trying to determine that the form string is not empty? In that case you want to use len()

    1 reply

    Inspiring
    April 22, 2015

    Your options in the dropdown are blank????

    Inspiring
    April 22, 2015

    Sorry I was trying to short cut the coding.  There are actually options in the drop down.

    Like this...

    <form method="post" action="">
         <select name="sector">

              <option value="">All Personnel</option>

              <option value="Air Force">Air Force</option>

              <option value="Army">Army</option>

              <option value="Navy">Navy</option>

              etc...

         </select>

         <select name="status">

              <option value="">All Statuses</option>

              <option value="Active">Active</option>

              <option value="Completed">Completed</option>

              etc...

         </select>

         <input type="submit" />

    </form>

    Anit_Kumar
    Community Manager
    Community Manager
    April 22, 2015

    You can try this:-

    <form method="post" action="">

         <select name="sector">

              <option value="">All Personnel</option>

              <option value="Air Force">Air Force</option>

              <option value="Army">Army</option>

              <option value="Navy">Navy</option>

              etc...

         </select>

         <select name="status">

              <option value="">All Statuses</option>

              <option value="Active">Active</option>

              <option value="Completed">Completed</option>

              etc...

         </select>

         <input type="submit" />

    </form>

    <cfif IsDefined("form.sector")>

      <cfoutput>sector='#sector#' and status='#status#'</cfoutput>

    </cfif>

    Regards,

    Anit Kumar