2010-09-19 9 views
10

Come posso sviluppare un sistema di avviso come Facebook, dove Utente A aggiungi Utente B, Utente B otterrà un certo numero nella sezione Richiesta amico nell'intestazione come nell'immagine qui sotto. Come posso sviluppare qualcosa del genere? Come possiamo ottenere numeri come questo ?? come posso ottenere i codici in PHP e JQuery?
alt textCome sviluppare un sistema di avviso come Facebook usando PHP e Jquery?

risposta

17

Presumo che si desidera un mezzo di allertare l'utente A, quando l'utente B 'amici' lui/lei senza la necessità di un aggiornamento della pagina?

Ciò richiede "AJAX". AJAX sta per Asynchronous Javascript e XML, ma questo è un termine sovraccarico ora-a-giorni con la reale struttura dei dati di scambio che spesso usa JSON invece di XML. JSON è JavaScript Object Notation. Ad ogni modo, l'idea è che la tua pagina web - senza essere aggiornata - possa effettuare chiamate periodiche al tuo server per ottenere informazioni nuove o aggiornate per aggiornare il display. Con PHP e jQuery, ti consigliamo di impostare prima la chiamata AJAX sulla tua pagina come questa:

$(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 > 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. 
} 

}); 

Questa volontà, ogni 15 secondi, fare una richiesta al server presso l'url /check.php su lo stesso dominio dell'origine della pagina web. Il PHP dovrebbe interrogare il tuo database e restituire il numero di richieste di amici non letti. Forse qualcosa del genere:

<?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' => $num_friend_requests, 
         'messages' => $num_unread_messages ]; 

     return json_encode($response); 
     exit; 

     case 'someOtherMethodIfRequired' : 
     // ... 
     exit; 
    } 
?> 
+0

Non capisco, puoi spiegare un po 'di più? –

+0

Come è? =) – mkoistinen

+0

thks ..... questo aiuta davvero ... –

Problemi correlati