我有一个简单的ajax脚本,可以在数据库中插入数据。效果很好。当我使用setInterval
每隔3秒从log.php
查询数据时,我会得到未定义的索引。这是合乎逻辑的,因为没有数据被发送。我怎么能避免这种情况?如何在没有log.php
的{{1}}上传递所需的变量来成功运行选择查询?
log.php
onclick
的Ajax
//get variables
$chatMessage= $_POST['chatMessage'];
$fromUserId= $_POST['fromUserId'];
$toUserId= $_POST['toUserId'];
$chatStatus='1';
$loginid_session=$fromUserId;
$fromUser=$toUserId;
$chatQ="SELECT * FROM ve_chat c JOIN ve_users u ON c.fromUserId=u.UserID WHERE c.toUserId= '$loginid_session' AND c.fromUserId ='$fromUser' ORDER BY sent DESC";
$chatresult=mysqli_query($db,$chatQ);
while($chats=mysqli_fetch_array($chatresult)){
echo "<div id='displayMessage'>";
echo "<span>" . $chats['ProfileName'] . " sent on" .$chats['sent']."</span> : <p >".$chats['message']."</p>";
echo "</div>";
}