wuno
wuno

Reputation: 9865

Using If Else Statements To Notify user Of Results AJAX

I have a search function which executes on keyup. The messages could be

  1. Shows loading when there are results
  2. Reloads full table when user deletes values or does not add any chars to the search field. (This scenario recalls on the displayRecords() function.)
  3. Tells the user there are no results.

I cannot seem to get the third scenario to work. My current code will shows "These are your search results..." and it will call the function to populate the table displayRecords() if the user deletes the chars they have entered. How can I get the program to take all 3 of my scenarios into its logic.

$("#itemID").keyup(function (){
    var url = searchPath;
    $.ajax({
        type  : "POST",
        async : false,
        url   : url,
        data: $('#itemID').serialize(),
        cache : false,
        success: function(html) {
           $( "#productResults" ).html( html );
        if (html === null) {
                  $("#loader_message").html('<p>There were no results that match your search criteria</p>').show();
              } else {
                  $("#loader_message").html('These are your search results... <img src="../wp-content/uploads/2016/02/loading.gif" alt="Loading">').show();
              }
        if( !$('#itemID').val() ) {                     
                displayRecords(limit, offset);   
            }
                html = null;
                window.busy = false;
            }
      });
});    

Upvotes: 0

Views: 51

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

probably your variable html is not null, using jQuery.trim() to remove extra whitespaces from your response data, so that you can check for emptiness, as

..
if ($.trim(html) === '') { //check if its blank
    $("#loader_message").html('<p>There were no results that match your search criteria</p>').show();
} else {
    $("#loader_message").html('These are your search results... <img src="../wp-content/uploads/2016/02/loading.gif" alt="Loading">').show();
}
..

Upvotes: 1

Related Questions