I'm having problems with a "load more button". I try to show the second page of results (and the others ahead) every time I make a new search, but the value I use to determine which is the current page of results, is always increasing... And I need to reset it when user makes a new search
here's the code I'm using
html:
<input type="text" id="input"/>
<button id="search"> Search </button>
<div id="result"> </div>
js to send form:
<script type="text/javascript">
$('#search').on('click', function(){
var page_search = 1;
var search= $('#input').val();
showSearch(page_search);
});
$("#load-more").click(function () {
page_search++;
showSearch(page_search);
});
function showSearch(page_search){
$.post( 'search.php', {'page_search': page_search, 'search': search}, function(data){
if(data.trim().length == 0){
$("#result").show().html("no more results found");
}else if(page_search=1){
$("#result").html(data);
}else{
$("#result").append(data);
}
});
}
</script>
php file: is reciving the input text value and the current page to generate a limit to show the results, something like:
$item_per_page = 5;
$position = (($page-1) * $item_per_page);
$search= $_POST['search'];
$page=$_POST["page_search"];
$query=("SELECT columns from table oder by blabla DESC LIMIT $position, $item_per_page");
The problem is with page_search++ and how can I reset that variable each time I make a new search, otherwise is just increasing and increasing and never start how it should be, just at the first search but then is useless