【发布时间】:2023-03-20 22:21:01
【问题描述】:
我一直在尝试获取两个用户之间的聊天消息,并希望将它们放在两个不同的 divs 中并分别设置两个 div 的样式。我现在正在做的是获取聊天并将其放入单个 div“消息”中,但我想获取数据并将其放入“消息”div 中的两个 div 中。
function fetch_chat() {
// to fetch the chats between two users
$.ajax({
url: "fetch_chat.php",
method: "POST",
data: {
id: currentID
},
dataType: "text",
success: function(data) {
$("#messages").show();
$('#messages').html(data);
$("div.area").show();
//chatTimer = setTimeout(fetch_chat, 500); //request the chat again in 2 seconds time
if (!scrolled) {
$("#messages").animate({ scrollTop: $(document).height() }, "fast");
scrolled = true;
}
}
});
}
<?php
require("config.php");
$id = $_POST['id'];
$sql = "select * from users where id='$id'";
$res = mysqli_query($con, $sql);
$row = mysqli_fetch_array($res);
$user_to = $row['name'];
$sql1 = "select * from chats where user_from='$_SESSION[name]' AND user_to='$user_to' OR user_to='$_SESSION[name]' AND user_from='$user_to' order by id";
$res1 = mysqli_query($con, $sql1);
if (mysqli_num_rows($res1) > 0) {
while ($row = mysqli_fetch_array($res1)) {
echo $row['msg'];
}
} else
echo "No msgs <br />";
?>
【问题讨论】: