Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I presume you want a means of alerting user A, when user B 'friends' him/her without requiring a page refresh?</p> <p>This requires "AJAX". AJAX stands for Asynchronous Javascript and XML, but this is an overloaded term now-a-days with the actual exchange data structure often using JSON instead of XML. JSON is JavaScript Object Notation. Anyway, the idea is that your webpage--without being refreshed--can make periodic calls to your server to get new or updated information to update the display with. With PHP and jQuery, you'll want to first set up the AJAX call on your page like this:</p> <pre><code>$(function() { // on document ready function updateAlerts() { $.ajax({ url : "/check.php", type : "POST", data : { method : 'checkAlerts' }, success : function(data, textStatus, XMLHttpRequest) { var response = $.parseJSON(data); // Update the DOM to show the new alerts! if (response.friendRequests &gt; 0) { // update the number in the DOM and make sure it is visible... $('#unreadFriendRequestsNum').show().text(response.friendRequests); } else { // Hide the number, since there are no pending friend requests $('#unreadFriendRequestsNum').hide(); } // Do something similar for unreadMessages, if required... } }); setTimeout('updateAlerts()', 15000); // Every 15 seconds. } }); </code></pre> <p>This will, every 15 seconds, make a request to your server at the url /check.php on the same domain as the origin of the webpage. The PHP should query your database and and return the number of unread friend requests. Perhaps something like this:</p> <pre><code>&lt;?php function isValid(session) { // given the user's session object, ensure it is valid // and that there's no funny business // TO BE IMPLEMENTED } function sanitize(input) { // return CLEAN input // TO BE IMPLEMENTED } // Be sure to check that your user's session is valid before proceeding, // we don't want people checking other people's friend requests! if (!isValid(session)) { exit; } $method = sanitize($_POST['method']); switch ($method) { case 'checkAlerts' : // Check DB for number of unread friend requests and or unread messages // TO BE IMPLEMENTED $response = ['friendRequests' =&gt; $num_friend_requests, 'messages' =&gt; $num_unread_messages ]; return json_encode( $response ); exit; case 'someOtherMethodIfRequired' : // ... exit; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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