In principle, you want a container where to place all the newly fetched messages.
<div id="messages">
<! -- message 1 -->
<! -- message 2 -->
<div id="newMessages">
</div>
</div>
Then you want a button (or anchor, or whatever) that you can hook up to a fetch-more-method:
<input type="button" value="Fetch more messages" id="btnFetchMessages" />
Place the hookup somewhere inside DOMReady:
var lastFetchedMessage = 0;
$('#btnFetchMessages').click(function() {
$.ajax({
url: '/getMessages.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: '{"lastId":'+lastFetchedMesasge+'"}',
success: newMessagesReceived
});
So on a click of the button we submit an AJAX request to getMessages.php where you'll look at the lastFetchedMessage and return the 10 or so next messages based upon that. From that page, you return a JSON array containing the messages, with all their info that you're interested in.
Upon a successful AJAX reques, the funciton newMessagesReceived will be called:
function newMessagesReceived(result) {
var container = $('#newMessages');
for(var i = 0; i < result.length; i++) {
var message = $('<div>' + result[i].body + '</div>');
container.append(message);
}
var lastFetchedMessage = result[i].id;
}
The above assumes that you return an array with messages, that have at least the properties body and id:
[ { "body": "new message1", "id": "10" }, { "body": "new message2", "id": "11" } ]