You've got your window.scroll event handler and your document.ready event handler both defined within the search() function; so although search() is getting called, at that point there's no handlers that will fire to get your data.
You no longer need to handle document.ready anyway, since at that point the user hasn't made any selection so there's no value for the parameter to send back to the ajax function. I'm assuming that clicking the button should get the first results via ajax, then scrolling to the end will load more, and so on...?
In this case it's safe enough (I believe) to just make fired_button a global variable, set it within the button click event and access it from getData without needing to pass as a parameter, which really adds no value. In your getData function, do nothing if a button hasn't yet been selected (i.e. if the user scrolls down before clicking a button).
When the user clicks a button, you need to clear previous results and reset your start pointer and max flags.
So I think your code becomes
<script>
var fired_button = "";
var start = 0;
var limit = 50;
var reachedMax = false;
$("button").click(function() {
fired_button = $(this).val();
start = 0;
reachedMax = false;
$(".results").html("");
getData();
});
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height())
if (fired_button != "")
getData();
});
function getData() {
if (reachedMax)
return;
$.ajax({
url: 'data.php',
method: 'POST',
dataType: 'text',
data: {
getData: fired_button,
start: start,
limit: limit
},
success: function(response) {
if (response == "reachedMax")
reachedMax = true;
else {
start += limit;
$(".results").append(response);
}
}
});
}
</script>
As a general comment, since (I assume) there is only one results list, I'd use a unique ID and reference via #results rather than use the class name and .results ... Maybe it's just me, but I find using a specific ID makes it much clearer that you are referring to a unique element rather than a classname which may return multiple elements.