dr6673999 2013-09-05 14:09
浏览 47
已采纳

AJAX代码突然停止工作

So I have been making a chat and at 14:09 it was working fine, it displayed messages, received them and stored them in DB. It also displayed stored messages and showed users the messages instantly. However suddenly it just stop showing all the messages. I have checked and the messages are stored in the DB when sent. I dunno what the error is as I've not changed the code to affect it. It would help a lot if you could help me with this! :)

index.php (includes AJAX jQuery & form)

                <div id="messageSend">
                <form action="shout.php" method="post" id="chat">
                <textarea rows="8" cols="74" id="Message" name="Message" placeholder="Post messages here."></textarea> 
                <br/><input type="submit" id="submit" value="Shout!">
                </form>

                    <script>
                        $('#chat').submit(function(e) {  
                            e.preventDefault();  
                                var form = $('#chat'),  
                                url = form.attr('action'),  
                                Message = $('#Message');
                                $.post(url, {Message : Message.val()}, function(data) {  
                                $('#messageDisplay').html(data);  
                                $('#Message').empty().val('');                                    

                        });
                                                    setInterval(function() {
                              // Do something every 2 seconds

                        {
                            $.get("data.php").done(function(data) {
                            $('#messageDisplay').html(data);
                                    });
                                }
                            }, 1000);
                       });

                    </script>

shout.php (send message and adds to DB)

<?php

include 'auth.login.php';
include 'pdo.config.php';

if (!isset($_SESSION['Username'])) {
echo '<br/>';
echo '<center>You need to login to post!<br/>';
header("Refresh:2; URL=index.php");
exit();
}

if (!isset($_POST['Message']) || empty($_POST['Message'])) {
echo '<br/>';
echo '<center>Message box empty!<br/>';
exit();

}

$Username = $_SESSION['Username'];

$Message = htmlspecialchars(trim($_POST['Message']));

$insertMessage = $PDO->prepare("INSERT INTO `chatbox` (User, Message) VALUES (?, ?)");
$insertMessage->bindParam(1, $Username, PDO::PARAM_STR);
$insertMessage->bindParam(2, $Message, PDO::PARAM_STR);
$insertMessage->execute();


?>

data.php (file which has loop to retrieve messages from DB)

<?php

include 'pdo.config.php';

$chat = $PDO->query("SELECT * FROM `chatbox`");

while($getRow = $chat->fetch(PDO::FETCH_ASSOC)) {  
     echo '['.date('d/m/Y g:i:s A',  strtotime($getRow['SentOn'])).'] '.$getRow['User'].': '.stripslashes($getRow['Message']).'<br/>';  
}

?>
  • 写回答

1条回答 默认 最新

  • dsc80135 2013-09-05 14:42
    关注

    Your page data.php is not being called. The contents are these:

    [05/09/2013 10:37:31 AM] test: test [05/09/2013 10:37:44 AM] test: test

    So, they are being inserted properly; however, when a user clicks "Shout" it does not call the setInterval().

    Edit: Your problem is that after they login, the setInterval() isn't called. If you refresh the page after logging in, it will work (works for me).

    Try this:

    $(document).ready(function() {
        $('#chat').submit(function(e) {  
            e.preventDefault();  
            var form = $('#chat');
            url = form.attr('action'); 
            Message = $('#Message');
            $.post(url, {Message : Message.val()}, function(data) {  
                $('#messageDisplay').html(data);  
                $('#Message').empty().val('');                                    
            });
    
            setInterval(function() {
            // Do something every 2 seconds
                $.get("data.php").done(function(data) {
                    $('#messageDisplay').html(data);
                });
            }, 1000);
        });
    
        <?php if (isset($_SESSION['Username'])) { ?>
            setInterval(function() {
            // Do something every 2 seconds
                $.get("data.php").done(function(data) {
                    $('#messageDisplay').html(data);
                });
            }, 1000);
        <?php } ?>
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?