<!DOCTYPE html>
<html>
<head><title>Data Notifications</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.css">
<script>
var oTable;
var rowcount = 0;
$(document).ready(function() {
oTable = $("#upd").dataTable({
"bServerSide": true,
"sAjaxSource": "cgi-bin/load.cgi",
} );
rowcount = oTable.fnSettings().fnRecordsTotal();
} );
setInterval(function() {
oTable.fnDraw();
var cur_count = oTable.fnSettings().fnRecordsTotal();
if(cur_count != rowcount){
notifyMe();
rowcount = cur_count;
}
}, 1000);
</script>
<script>
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check if the user is okay to get some notification
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Data Updated!");
}
// Otherwise, we need to ask the user for permission
// Note, Chrome does not implement the permission static property
// So we have to check for NOT 'denied' instead of 'default'
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// Whatever the user answers, we make sure we store the information
if(!('permission' in Notification)) {
Notification.permission = permission;
}
// If the user is okay, let's create a notification
if (permission === "granted") {
var notification = new Notification("Data Updated");
}
});
}
// At last, if the user already denied any notification, and you
// want to be respectful there is no need to bother him any more.
}
</script>
</head>
<body>
<table id="upd" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Slno</th>
<th>Name</th>
</tr>
</thead>
</table>
<form method='GET' onclick = "notifyMe();">
<input type ="submit" name ="sub" value ="LoadTable">
</form>
</body>
</html>
------------------------------------------------------
#!C:/perl/bin/perl.exe
use strict;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use CGI;
use JSON;
use DBI;
use Data::Dumper;
my $cgi = new CGI;
print $cgi->header();#'application/json');
#print $cgi->header(
# -Cache-Control => 'no-cache',
# );
#print $cgi->start_html();-title=>'Table Updates');
#open my $out, ">", "hhaha";
my $sech = $cgi->param("sEcho");
#print $out $sech; exit;
my $dbh = DBI->connect('dbi:mysql:medicines', 'root');
my $sql = q/select slno, name from med_stock/;
my $sth = $dbh->prepare($sql);
$sth->execute;
#print $cgi->table({border=>1});
# table headings are SQL column names
my %hash;
$hash{'draw'} =1;
$hash{'aaData'} = ();
$hash{'sEcho'} = $sech;
my $i =0;
#print "
$sth->{NAME}->[0]
$sth->{NAME}->[1]
";
while (my @row = $sth->fetchrow_array) {
#print "
$row[0]
$row[1]
\n";
$hash{'aaData'}->[$i]->[0] =$row[0];
$hash{'aaData'}->[$i]->[1] =$row[1]; $i++;
}
#print Dumper(%hash);
$hash{"recordsTotal"}=$i;
$hash{"recordsFiltered"}=$i;
$hash{'iTotalRecords'} = $i;
$hash{'iTotalDisplayRecords'} = $i;
print encode_json(\%hash);
#print $cgi->end_table;
#print $cgi->end_html;
