Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Alert system (facebook notifications)

    Hi!

    I have a menu bar, and i want to display notifications like facebook every X seconds, show the count of a query and display the number in a div...

    Do you have some advice for me?

    Thanks a lot!!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Where does this information come from? Is it server side information coming from a file/database? Or is it already available in Javascript?

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    from database, is an alert system, query the table of alerts and show the count of new alerts....like the notifications of facebook...

    Thanks!

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    This is a typical Ajax use case. If you google for "simple Ajax example" you will find a lot of stuff for a start. If you still experience problems, please feel free to come back with your code.

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I found this code:

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
    
    function updateAlerts() {
       $.ajax({
          url : "administrator/check_alarmas.php?userid=<?php echo $id;?>",
          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...
                $('#alarmas').show().text(response.friendRequests);
             }
             else {
                // Hide the number, since there are no pending friend requests
                $('#alarmas').hide();
             }
    
             // Do something similar for unreadMessages, if required...
          }
       });
       setInterval('updateAlerts()', 5000); // Every 15 seconds.
    }
    
    });
    </script>
    and the check_alarmas.php:
    Code:
    <?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
       //}
    $id = $_GET['userid'];
       // 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; }
       session_start();
       if(empty($_SESSION['u_name']))
    	header("Location:index.php");	
    
      $method = $_POST['method'];
    
      switch ($method) {
         case 'checkAlerts' :
    	  
    	require( "connect.php" );		
    
             // Check DB for number of unread friend requests and or unread messages
             // TO BE IMPLEMENTED
    		 $alarmas = "SELECT 
    		 alarmas.serverid,
    		 owndev.serverid,
    		 owndev.userid
    		  FROM 
    		  alarmas,
    		  owndev
    		  WHERE
    		  owndev.serverid = alarmas.serverid
    		  AND
    		  owndev.userid = '2'";
    
    		$result_alarmas=mysql_query($alarmas) or die("Query_alarmas: <br />Error: ".mysql_error());
    		$row_alarmas= mysql_num_rows($result_alarmas);
            // $response = ['friendRequests' => $num_friend_requests,
                         // 'messages' => $num_unread_messages ];
    					echo $row_alarmas;
    					  
    		//$reponse2[]  = array('friendRequests' => $row_alarmas);        
    //}
    
            print json_encode (array( 'friendRequests' => $row_alarmas ));
             exit;
    
          //case 'someOtherMethodIfRequired' :
             // ...
            // exit;
      }
    ?>
    But it doesnt work, any advice?

    Thanks!

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    You are not calling updateAlerts() anywhere ... so the recurring interval of 5 seconds never starts


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •