Reputation: 4718
I am making a load more paginate button, I'm stuck at two things that are both similar. I don't have a way of increasing pages, and I don't have a way for the script to know when the paginate is finished. Here is my jQuery code for the load more:
$(document).on('click', '#loadmore', function() {
$.get('/artistprofileloadmore.php?page=2&aid=$aid', function(data) {
$("#append").append(data);
});
});
I can easily solve this if there is a way for the jQuery to receive a variable value from the php file. So is there? Simply put, if the paginate is finished, the variable $end=1 is set, otherwise it is set to 0, is there a way I can use the jQuery to check when the variable $end==1 and then I would remove the div #loadmore to remove the next page button.
For the next page, is there a way that I can set jQuery to increase a variable every time it is run, which will increase the page number? The variable would be used instead of this two:
?page=2
and increase every click
Upvotes: 0
Views: 372
Reputation:
I think on the PHP page you want to divide the content logically into the html code to be loaded into your container and also a variable containing whatever it is you want the php to send along with this page content. You can have, ex,
$html = [whatever];
$otherData = [other stuff I need to send along with html];
$return['html'] = $html;
$return['otherData'] = $otherData;
echo json_encode($return);
You then have to tweak the javascript to be able to receive this data. I suggest reading up on the JQuery AJAX API.
(I'm fond of JSON, but it's just one option for formatting the data the server sends.)
Upvotes: 1
Reputation: 4002
From PHP return a JSON like this
{"artists"="The Artists profiles","isLast"=true}
var pageNum = 1;
$(document).on('click', '#loadmore', function () {
$.get('/artistprofileloadmore.php?page=' + pageNum + '&aid=$aid', function (data) {
$("#append").append(data.artists);
if(data.isLast){
$('#loadmore').hide();
}else{
pageNum++;
}
});
});
Upvotes: 2