新しいデータをチェックし続け、データベースに新しいデータが見つかった場合に追加するために ajax 時間関数を使用しています。
PHP関数(load.php)
$tocom=$_POST['tocom'];
$sql=mysqli_query($db3->connection,"SELECT * FROM chat_com where to='$tocom' ORDER by time DESC");
while($row=mysqli_fetch_array($sql)){
$tocomr=$row['tocom'];
$text=$row['text'];
echo $text;
}
Ajax関数はこちらです。
function chat_com_one(id, name) {
$('#chatcom').show('fast');
(function chatcom_load_one(id, name) {
$.post('load.php', {tocom:id}, function(data) {
$('#chat_win').append(data);
setTimeout(chatcom_load_one(id, name), 500);
});
}(id, name));
}