我试图仅借助JQuery的设置间隔和加载功能来刷新div。正在与数据库一起获取数据。我希望它重新加载,因为这是聊天门户
我试图在#chat中加载chat1.php代码:
<?php
include('db.php');
$ment=$_GET['mentor_name'];
$stu=$_GET['stu'];
?>
<?php
if(isset($_POST['stu_send'])){
$msg_student=$_POST['msg_student'];
$sqls=mysqli_query($con,"insert into chats (student_email,mentor_email,content,sender_id) values ('$stu','$ment','$msg_student','$stu')");
echo '<a href="chat1.php?stu=<?php echo $stu; ?> & mentor=<?php echo $ment; ?>"></a>';
}
$chat=mysqli_query($con,"select content,sender_id,c_date from chats where student_email='$stu' AND mentor_email='$ment'");
while($row_con=mysqli_fetch_array($chat)){
$sender_id=$row_con['sender_id'];
$cont=$row_con['content'];
$c_date=$row_con['c_date'];
echo $sender_id.": ";
echo $cont . "<br/>";
echo $c_date;
// echo date("h:i:sa");
// echo date("Y/m/d");
echo "<hr>";
}
?>
刷新代码:
setInterval(refreshMessages, 1000);
function refreshMessages() {
$.ajax({
url: 'chat1-1.php',
type: 'GET',
dataType: 'html',
success: function(data) {
$('#chat').html(data); // data came back ok, so display it
},
error: function() {
$('#chat').prepend('Error retrieving new messages..');
}
});
}