I'm studying ajax, jquery and php. And with the goal of making a screen that loads automatically, for future chatting. What I want to understand is the screen loading function.
I have the index.php code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
require "bd.php";
?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Ajax With Jquery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"type="text/javascript" charset="utf-8"></script>
<script>
function autoRefresh_div()
{
$("#result").load("index.php").show();// a function which will load data from other file after x seconds
}
setInterval('autoRefresh_div()', 4000);
</script>
</head>
<body>
<div id="result">
<?php
if($sql->rowCount() > 0){
foreach($sql->fetchAll() as $dados){
echo "mensagem: ".$dados['mensagem']."<br>";
}
}
?>
</div>
</body>
</html>
I have the bd.php code
<?php
$dsn = "mysql:dbname=realtime;host=localhost";
$dbname = "root";
$dbpass = "";
try{
$pdo = new PDO($dsn, $dbname, $dbpass);
}catch(PDOException $e){
echo "Erro: ".$e->getMessage();
}
$sql = "SELECT * FROM chat";
$sql = $pdo->query($sql);
?>
The problem that according to time, the screen hangs because it sends several requests and this is leaving the server heavy and locks the navigation screen. Is there anything I can improve on the code to avoid this problem?