Jump to content

Load more content button JS and PHP


Go to solution Solved by Adamhumbug,

Recommended Posts

Hi All,

I have a function that pulls data in php form my db and uses a limit and an offset.  This is currently called like this

<?=getAllNews(0,1)?>

with 0 being the offset and 1 being the limit.

The line after this is a button triggering ajax

<div class='btn btn-secondary w-100 text-center loadMoreNewsTrigger' data-limit='1' data-offset='1'>-- Load More News --</div>

and the ajax

$('.loadMoreNewsTrigger').click(function(){
	var offset = $(this).data('offset');
	var limit = $(this).data('limit');
	
	$.ajax({
		type: 'post',
		data: {"ajax" : 'one', "offset" : offset, "limit" : limit},
		success: function(resp){
			offset = offset + 1
			limit = limit + 1
			$('.moreNews').append(resp + "<div class='btn btn-secondary w-100 text-center loadMoreNewsTrigger' data-limit='limit' data-offset='$offset'>-- Load More News --</div>")
			
		}
	})
});

 

I am trying to increment the data values before appending the new button to the end but im getting tangled up.

 

help is always appreciated

Link to comment
https://forums.phpfreaks.com/topic/311321-load-more-content-button-js-and-php/
Share on other sites

  • Solution

i went this route in the end and didnt bother with data attributes.

function getMoreNews(limit, offset){
	$.ajax({
		type: 'post',
		data: {"ajax" : 'one', "offset" : offset, "limit" : limit},
		success: function(resp){
			offset = offset + 1
			limit = limit + 1
			$('.moreNews').append(resp + "<div class='btn btn-secondary w-100 text-center loadMoreNewsTrigger mb-3' onclick='getMoreNews(1,"+offset+");' data-limit='1' data-offset="+offset+">-- Load More News --</div>")
			
		}
	})
};

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.