所以我正在尝试创建一个php mysql聊天,我在一个页面上成功使用javascript load(),但在实际的聊天页面上,我无法让它工作b / c每个聊天都有它是在附加到URL的聊天ID(ch_id)上(所以它看起来像chats.php?ch_id =#)...当我尝试用load()自动刷新聊天窗口时,它不会带来聊天ID ,所以页面刷新为空白。有什么想法吗?
代码:HTML
<div id="load_chat">
<?php include('load_chat.php'); ?>
</div>
PHP - load_chat.php文件
<div class="scroller" style="height: 525px;" data-always-visible="1" data-rail-visible1="1">
<ul class="chats">
<?php
$chat = "SELECT * FROM chats WHERE ch_id = '".$ch_id."'";
$r_ch = mysqli_query($con, $chat);
$w_ch = mysqli_fetch_array($r_ch);
$get_chat = "SELECT * FROM chat_log WHERE ch_id = '".$ch_id."'";
$run_chat = mysqli_query($con, $get_chat);
while($row_chat = mysqli_fetch_array($run_chat)) {
$get_sender = "SELECT * FROM users WHERE u_id = '".$row_chat['u_id']."'";
$run_sender = mysqli_query($con, $get_sender);
$row_sender = mysqli_fetch_array($run_sender);
$get_rec = "SELECT * FROM users WHERE u_id = '".$row_chat['r_id']."'";
$run_rec = mysqli_query($con, $get_rec);
$row_rec = mysqli_fetch_array($run_rec);
?>
<li class='in'>
<img class='avatar' alt='' src='images/<?php echo "".$row_sender['u_img'].""; ?>' />
<div class='message'>
<a href='javascript:;' class='name'> <?php echo "".$row_sender['u_fn']." ".$row_sender['u_ln'].""; ?> </a>
<span class='datetime'> <?php echo "".date("g:i a", strtotime($row_chat['chat_sent'])).""; ?> </span>
<span class='body'> <?php echo "".$row_chat['c_cont'].""; ?> </span>
</div>
</li>
<?php
}
?>
</ul>
</div>
JAVASCRIPT
<script>setInterval(function() { ('#load_chat').load('load_chat.php'); }, 2000)</script>
澄清...'()。load()函数正在运行......但是当它刷新时,它正在丢失聊天ID(.com?ch_id =#在URL上)...我猜我的主要问题,有没有办法将聊天ID传递给子文件夹/文件?页面刷新特定页面(当一个新的聊天组启动时,创建一个文件,其中有php / mysql查询以获取所有记录的聊天记录)我需要将聊天ID传递给那些页面...我该怎么做去做?
感谢所有已经插话的人...我非常感谢我从这个社区获得的所有帮助!
答案 0 :(得分:0)
在您的主PHP文件中,将聊天室的ID转移到这样的JavaScript(最好在<head>
中):
<script>var chat_id = <?= $ch_id ?>;</script>
现在使用
setInterval(function() { ('#load_chat').load('load_chat.php?ch_id=' + chat_id); }, 2000)