long pulling if record is new
i have one table in my db.
Employee
___________________________________
| Name | City |
___________________________________
| Jack | UK |
___________________________________
| John | Kenya |
___________________________________
and i am use select query to get value from this table.
this is my sql.php file for select data from Employee.
<?php
$con=mysqli_connect("localhost","root","","company");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM employee");
while($row = mysqli_fetch_array($result))
{
echo $row['name'] . " " . $row['city'];
}
mysqli_close($con);
?>
and now i am try long pulling this is simple code of long pulling:-
<html>
<head>
<title>BargePoller</title>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"
type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">
body{ background:#000;color:#fff;font-size:.9em; }
.msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}
.old{ background-color:#246499;}
.new{ background-color:#3B9957;}
.error{ background-color:#992E36;}
</style>
<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
/* Simple helper to add a div.
type is the name of a CSS class (old/new/error).
msg is the contents of the div */
$("#messages").append(
"<div class='msg "+ type +"'>"+ msg +"</div>"
);
}
function waitForMsg(){
/* This requests the url "msgsrv.php"
When it complete (or errors)*/
$.ajax({
type: "GET",
url: "sql.php",
async: true, /* If set to non-async, browser shows page as
"Loading.."*/
cache: false,
timeout:1000, /* Timeout in ms */
success: function(data){ /* called when request to barge.php
completes */
addmsg("new", data); /* Add response to a .msg div (with
the "new" class)*/
setTimeout(
waitForMsg, /* Request next message */
1000 /* ..after 1 seconds */
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg, /* Try again after.. */
1000); /* milliseconds (15seconds) */
}
});
};
$(document).ready(function(){
waitForMsg(); /* Start the inital request */
});
</script>
</head>
<body>
<div id="messages">
<div class="msg old">
BargePoll message requester!
</div>
</div>
</body>
</html>
its work perfect and ajax call every second.
but i want to set if any changes are made in table then its append div on
my page other wise nothing.
how can i do this.
thanks.
No comments:
Post a Comment