0

I need to display no results found if the search term by the user doesn't matches any result from the database. I have gone through various questions on here, but none helped me.

<script>
  $(function() {
    function log( message ) {
      $( "<div>" ).text( message ).prependTo( "#log" );
      $( "#log" ).scrollTop( 0 );
    }

    $( "#birds" ).autocomplete({
      source: "search.php",
      minLength: 2,
      select: function( event, ui ) {


        log( ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.label :
          "Nothing selected, input was " + this.actor );
         window.location.href = './company.php?id=' + ui.item.indexid + '&name=' + ui.item.label;

      }
    });
  });



  </script>

Any suggestion would be helpful.

Huangism
  • 16,278
  • 7
  • 48
  • 74
  • What's `search.php`? Do you see anything if you look in your browser's network tab in the dev tools? – gen_Eric Jul 16 '14 at 18:18
  • is this jqueryUI, or what plugin are you using? – dave Jul 16 '14 at 18:19
  • @dave: I'm assuming jQuery UI since the code shown is just the code from the example page: http://jqueryui.com/autocomplete/#remote – gen_Eric Jul 16 '14 at 18:20
  • Look at these: [Detecting no results on jQuery UI autocomplete](http://stackoverflow.com/questions/4718968/detecting-no-results-on-jquery-ui-autocomplete) and [jQuery UI autocomplete, show something when no results](http://stackoverflow.com/questions/4141945/jquery-ui-autocomplete-show-something-when-no-results). – j809 Jul 16 '14 at 18:20
  • search.php is the file which processes the search term. – user3727339 Jul 16 '14 at 18:36
  • Why isn't this line getting displayed `"Nothing selected, input was " + this.actor` when there are no results. – user3727339 Jul 16 '14 at 18:37
  • @user3727339: What does it look like? What does it print? – gen_Eric Jul 16 '14 at 18:42
  • @user3727339: What debugging have you tried? What does `console.log(ui);` show? – gen_Eric Jul 16 '14 at 18:42

1 Answers1

1

Here is a FIDDLE in which you loop through the returned data inside of "response:" and when you get an array with length == 0, it fills in the input field with "No matches" or anything else you'd want.

And just to throw out an idea, if the dataset is small enough, you can read the all the data from the db, then do the autocomplete.

JS

var areas = ['california', 'nevada', 'oregon', 'utah', 'arizona', 'new mexico', 'minnesota', 'texas', 'louisiana', 'alabama', 'mississippi', 'oklahoma'];

$('.autocomp').autocomplete({
    autoFocus : true,
    source : areas,
    selectFirst : true,
    response: function( event, ui ) {
                                     var count = 0;
                                     console.log( ui.content.length );
                                     for (var n=0; n < ui.content.length + 1; n++)
                                     {
                                      if( ui.content.length == 0 )
                                      {
                                       $('.autocomp').val('No results');
                                       } else {
                                       count = count+1;
                                               }
                                       }
                                      }
});
TimSPQR
  • 2,964
  • 3
  • 20
  • 29