I have an AJAX function which populates a table as the user searches. Each keyup provides new results based on their search criteria.
I want to validate the search to keep users from excessively triggering the event which obviously could cause tons of problems. But I do need the functionality of the list repopulating as the user searches or types.
How can I provide validation to protect the server and make the code overall more professional.
AJAX var searchPath = "";
$("#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;
}
});
});
FORM
<div class="form-group pull-right">
<input type="text" name="itemID" id="itemID" class="search form-control" placeholder="Search product number">
</div>
CONTENT AREA
<tbody id="productResults">
</tbody>
</table>
<div id="loader_image"></div>
<div id="loader_message"></div>