...

View Full Version : Alert system (facebook notifications)



rolperez
01-23-2012, 11:09 PM
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!!

devnull69
01-24-2012, 07:22 AM
Where does this information come from? Is it server side information coming from a file/database? Or is it already available in Javascript?

rolperez
01-24-2012, 07:45 AM
from database, is an alert system, query the table of alerts and show the count of new alerts....like the notifications of facebook...

Thanks!

devnull69
01-24-2012, 08:27 AM
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.

rolperez
01-24-2012, 07:25 PM
I found this 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:


<?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!

devnull69
01-25-2012, 11:47 AM
You are not calling updateAlerts() anywhere ... so the recurring interval of 5 seconds never starts



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum