Skip to main content
Inspiring
April 30, 2007
Question

search - no results

  • April 30, 2007
  • 5 replies
  • 465 views
hi guys,

how can i output a "no results found" or similar if my search doesnt return anything?

Any help would be appretiated!


cheers

James
    This topic has been closed for replies.

    5 replies

    Inspiring
    May 7, 2007
    On the "search results" page, check for the existence of the form field and make sure it contains a value before processing the search:
    <cfif IsDefined("FORM.searchCriteria") AND Trim(FORM.searchCriteria) IS NOT "">
    <!--- perform search --->
    <cfelse>
    <!--- show error/search form not completed message -->
    </cfif>

    It's also a good idea to incorporate some basic client-side validation on the search form itself.

    If you have CFMX 6+, you can use cfform, which will write the necessary JavaScript for you.
    <cfform name="searchform" method="post">
    <cfinput type="text" name="searchCriteria" required="true" message="You must enter a criteria/keywords to perform a search"/>
    </cfform>

    If you don't have CFMX 6+, you probably need to write some basic JavaScript form validation script (I cannot recall what CF 5 or earlier did for this).
    RookOrnAuthor
    Inspiring
    May 7, 2007
    thanks for your replies guys.

    The recordcount will work if something is not found in the database, but am i right in saying it won't help if nothing is typed into the search box?

    any ideas on how to give your own error message if nothing is typed in. Otherwise currently it will just go to a CF error...
    Inspiring
    May 1, 2007
    yes, but <cfif yourquery.recordcount is 0> or <cfif
    yourquery.recordcount eq 0> - you must use cf operators in cfif
    --

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com
    RookOrnAuthor
    Inspiring
    May 1, 2007
    thanks for your reply.

    will something like:

    <cfif search.RecordCount=0>

    <cfoutput>Sorry no results</cfoutput>
    </cfif>

    work??
    April 30, 2007
    Check query_name.RecordCount variable and see if it is 0 or not.