Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In principle, you want a container where to place all the newly fetched messages.</p> <pre><code>&lt;div id="messages"&gt; &lt;! -- message 1 --&gt; &lt;! -- message 2 --&gt; &lt;div id="newMessages"&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Then you want a button (or anchor, or whatever) that you can hook up to a fetch-more-method:</p> <pre><code>&lt;input type="button" value="Fetch more messages" id="btnFetchMessages" /&gt; </code></pre> <p>Place the hookup somewhere inside DOMReady:</p> <pre><code>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 }); </code></pre> <p>So on a click of the button we submit an AJAX request to <code>getMessages.php</code> where you'll look at the <code>lastFetchedMessage</code> and return the 10 or so next messages based upon that. From that page, you return a <a href="http://json.org/" rel="nofollow noreferrer">JSON</a> array containing the messages, with all their info that you're interested in.</p> <p>Upon a successful AJAX reques, the funciton <code>newMessagesReceived</code> will be called:</p> <pre><code>function newMessagesReceived(result) { var container = $('#newMessages'); for(var i = 0; i &lt; result.length; i++) { var message = $('&lt;div&gt;' + result[i].body + '&lt;/div&gt;'); container.append(message); } var lastFetchedMessage = result[i].id; } </code></pre> <p>The above assumes that you return an array with messages, that have at least the properties <code>body</code> and <code>id</code>:</p> <pre><code>[ { "body": "new message1", "id": "10" }, { "body": "new message2", "id": "11" } ] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload