Inbox message should flash when new message come through

Discussion in 'PHP' started by mrkool, Mar 30, 2010.

  1. #1
    How to do this:
    I want to update my Inbox script of my custom made site which is about social networking.

    When new message come through It should update message list and total number of message instantly (Like Gmail and yahoo do). I am using PHP.

    Currently:
    When a member is logged in the inbox, it does not notify new incoming messages until the next log in again.

    What i want:
    Inbox message should flash when new message come through

    Can anyone guide me how to achieve this.
     
    mrkool, Mar 30, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    You have to do this via ajax. Check jQuery. That is easy powerful and exactly what you need.
     
    stephan2307, Mar 30, 2010 IP
  3. mrkool

    mrkool Active Member

    Messages:
    261
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #3
    Can you provide me any reference to this with sample script. Off-course I am also searching in google
     
    mrkool, Mar 30, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    If the user is not logged in ?

    Is it a global message for all users ?
     
    MyVodaFone, Mar 30, 2010 IP
  5. mrkool

    mrkool Active Member

    Messages:
    261
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #5
    Only for the members who logged in. Like any private message system.
     
    mrkool, Mar 30, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    Well couldnt you use an if statement and then just show an animated gif ?
     
    MyVodaFone, Mar 30, 2010 IP
  7. mrkool

    mrkool Active Member

    Messages:
    261
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #7
    If we use if statement, it will refresh whole page.
     
    mrkool, Mar 30, 2010 IP
  8. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #8
    OK this is something very basic that shows the idea behind the such a system


    Assume you have a div where it shows the inbox like this
    
    <div id="inbox">Inbox</div>
    
    HTML:
    Then you would include the jquery file in the header and have a javascript like this

    
    
    <script type="text/javascript">
    
    function checkMessages() {
       $('#inbox').load('ajax/messages.php');
       setTimeout("checkMessages();",10000);
    }
    
    $(window).load(function () {
    
      setTimeout("checkMessages();",10000);
    
    }
    
    </script>
    
    
    HTML:
    Then all you need is the file ajax/messages.php which would have something like this

    
    <?php
    mysql_connect(host,user,pass);
    mysql_select_db(dbname);
    
    $sql = 'SELECT COUNT(*) as `tot` FROM `messages` WHERE `user`='.$_SESSION['userid'].' AND `status`=0';
    $query = mysql_query($sql);
    $result = mysql_fetch_assoc($query);
    
    if ($result['tot']==0) { echo 'Inbox'; } 
    else { echo '<b>Inbox ('.$result['tot'].')</b>';}
    
    ?>
    
    PHP:
    This above example simply changes the "Inbox" to "Inbox (2)" and makes it bold. It checks that every 10 seconds.

    This should give you a good idea how to do it.
     
    stephan2307, Mar 30, 2010 IP
  9. mrkool

    mrkool Active Member

    Messages:
    261
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #9
    Thank you so much stephan2307
     
    mrkool, Mar 30, 2010 IP
  10. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #10
    no problem
     
    stephan2307, Mar 30, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    Alternatively you can do if a statement to check if theirs any unread messages (assuming you have a column for this), and if their is you could add <blink> tags.
     
    danx10, Mar 30, 2010 IP