Page 1 of 1
updating website/current page when it "notice" changes
Posted: Tue Jun 12, 2012 7:58 am
by headbull
To explain this easy, lets say I have a website chat. Where I want the "chat-window" to update when other users submit chat text. But only then, soo refresh every 10 second isnt what I am looking for.
I guess its some sort of AJAX together with PHP code, but how ?
Re: updating website/current page when it "notice" changes
Posted: Tue Jun 12, 2012 8:53 am
by TylerH4
It is not possible to "push" updates to the client without having a constant web-socket connection or polling via AJAX/jQuery every __ period of time.
http://www.websocket.org/index.html
Code: Select all
<?php
if(!is_null($_POST["update"])) {
// Code here
exit('{"msg":"Hello World","by":"Tyler"}');
}
?>
<!-- Add jQuery import -->
<script type="text/javascript">
function polling() {
$.ajax({
cache:false,
global:false,
timeout:2000,
type:"POST",
url:"/index.php",
dataType:"json",
success:updateSuccess,
error:dialogFail,
data:{"update":true}
});
}
function updateSuccess(result, status, xhr) {
$("#updateDiv").append(result.msg+"<br />By: "+result.by);
}
function dialogFail(xhr, status, error) {
alert("There was an error getting the previous update.");
}
setInterval(polling, 10000);
</script>
<div id="updateDiv"></div>