I'm trying to add pagination.
I use
$(document).ready(function() {
var pageNum = 1;
$(".paginationing").click(function(){
pageNum = $(this).attr("id");
});
setInterval("ajaxd("+pageNum+")",5000);
});
function ajaxd(pageNum) {
var thisuser = $("#thisusern").text();
$.ajax({
type: "GET",
url: "newstitle.php",
data: "user="+thisuser+"&page="+pageNum,
success: function(msg){
$("#edix").html(msg);
}
});
}
To send the page number.But it sends only page number 1 ,even I click on the page number.
Where's the bug?How can I implement AJAX pagination with setInterval to call the page every 5 seconds?
Am not sure why are you using setInterval to paginate instead of calling the ajaxd function in the click event handler.
I would do it this way:
$(document).ready(function() {
var pageNum = 1;
$(".paginationing").click(function() {
pageNum = $(this).attr("id");
ajaxd(pageNum);
});
});
function ajaxd(pgNo) {
var thisuser = $("#thisusern").text();
$.ajax({
type: "GET",
url: "newstitle.php",
data: "user=" + thisuser + "&page=" + pgNo,
success: function(msg) {
$("#edix").html(msg);
}
});
}
If you still want to use the setInterval try this:
var pageNum = 1;
$(document).ready(function() {
$(".paginationing").click(function() {
pageNum = $(this).attr("id");
});
setInterval(ajaxd, 5000);
});
function ajaxd() {
var pgNo = pageNum;
var thisuser = $("#thisusern").text();
$.ajax({
type: "GET",
url: "newstitle.php",
data: "user=" + thisuser + "&page=" + pgNo,
success: function(msg) {
$("#edix").html(msg);
}
});
}
P.S:
Also note that a valid id for a HTML element should not start with a number but I think you are relying on that. I would rather advice you to generate the id something like "pg-n" where n is the number and in the click event you can get the page number using
pageNum = $(this).attr("id").replace(/[^0-9]/g, "");