无法使用AJAX自动刷新页面元素

时间:2016-07-04 15:16:46

标签: php jquery ajax

我正在尝试制作聊天室,但我发现自己无法自动重新加载消息。 我正在使用数据库来处理消息,因为没有足够的权限让php在我的服务器上写文件。 这是聊天框的代码:

<input name="usermsg" id="usermsg" type="text"/>
    <button id="submitmsg" type="button" onclick="onSubmit()">Send</button>

</div>


<script type="text/javascript" src="src/assets/js/jquery.js"></script>

<script>
    function onSubmit(){
        //console.log('ghffgj');
        if($("#usermsg").val()) {
            //alert($("#usermsg").val());
            var clientmsg = $("#usermsg").val();
            $.ajax({
                type: "POST",
                url: "src/add_reply.php", 
                data: {text: clientmsg},
            });

            $("#usermsg").attr("value", "");
            return false;
        }
    }

    setInterval (loadLog, 3000);

    function loadLog(){   


        $.ajax({
            url: "src/chatlog.php",
            cache: false,
            success: function(data){        
                $("#chatbox").html(data);      
            },
        });
    }
</script>

以及chatlog.php的代码:

<?php
include('src/database_connect.php');

 $select_query = "SELECT author, send_date, message FROM chatbox";

 $chat_obj = $conn->query($select_query);

 while($row = $chat_obj->fetch_assoc()){
     echo "<div><span style='font-size: 10px'>(".$row['send_date'].")</span><b>".$row['author']."</b>: ".$row['message'];
 }

 $conn->close();

 ?>

在数据库中添加新消息有效,但我似乎无法将我在chatlog.php上显示的文本返回到聊天框页面

编辑:我忘了提,浏览器控制台显示如下: 获取http:// /chatbox/src/chatlog.php?_=1467644248226 500内部服务器错误 29ms

<小时/> 我找到了一个解决方案,如下所示: index.php(只有聊天室代码):

        <input name="usermsg" id="usermsg" type="text"/>
    <button id="submitmsg" type="button" onclick="onSubmit()">Send</button>

</div>


<script type="text/javascript" src="src/assets/js/jquery.js"></script>

<script>

    function onSubmit(){
        console.log($("#usermsg").val());
        if($("#usermsg").val()) {
            //alert($("#usermsg").val());
            var clientmsg = $("#usermsg").val();
            $.ajax({
                type: "POST",
                url: "src/add_reply.php", 
                data: {text: clientmsg},
            });

            $("#usermsg").val("");
            return false;
        }
    }



    function loadLog(){   


        $.ajax({
            url: "src/chatlog.php",
            cache: false,
            success: function(data){        
                $("#chatbox").html(data); 
            },
        })/*.done(function(msg) {
          alert(msg);
        })*/;
    }

    $( document ).ready(function() {
        loadLog();
        setInterval (loadLog, 2000);
    });
</script>

chatbox.php:

<?php

 include('database_connect.php');

 $select_query = "SELECT author, send_date, message FROM chatbox";

 $chat_obj = $conn->query($select_query);

 //$chat_list = array();
 $content = "";
 while($row = $chat_obj->fetch_assoc()){
     //$chat_list[] = $row;
      $content .= "<div><span style='font-size: 10px'>(" . 
         $row['send_date'].")</span><b>".
         $row['author']."</b>: ".$row['message']."</div>";
 }

 $conn->close();
 die($content);
 ?>

0 个答案:

没有答案