weixin_33695450 2015-07-13 17:56 采纳率: 0%
浏览 19

保持聊天刷新

I have a problem making my chat refresh constantly.

This is how I built it:

I save all messages in a MySQL database connected with PHP, loading the messages with PHP and post messages with PHP, I have jQuery making the message when clicked on submit send it to the chatbox.

My website with the chat

JSFIDDLE

My problem is that when me and a friend chat, we have to refresh the site everytime someone writes something. I want to make the chatbox refresh constantly. I have tried $('#example').load(document.URL + ' #example'); but it doesn't work, can someone please help me.

[HTML]

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>ChatBox</title>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js" charset="utf-8"></script>
    <script>
        $(document).ready(function(){
            $("#content").load("ajaxload.php");

            $("a.btn").click(function(){
                $.post('ajaxPost.php', $('#userarea').serialize(), function(data){
                        $('#content').append('<p>'+data+'</p>');
                });
                return false;
            });

        });
    </script>
    <script src="chatbox.js" charset="utf-8"></script>
    <link href='http://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="chatbox.css" title="ChatBox" type="text/css" media="screen" charset="utf-8">
</head>
<body>
    <h1 id="swegre">
        SWEGRE<span>&copy;</span>
    </h1>
    <div id="wrapper">
        <h1>Leave a comment!</h1>
        <div id ="content">
            <p style="background-color: #b7b7b7">Admin: Here's our chatbox</p>
        </div>

        <div id="message">
            <form id="userarea">
            <textarea id="textareamsg" name="messages" rows="2" cols="30" required=""></textarea>
            <a href="#" class="btn">POST</a>
            </form>
        </div>

    </div>
</body>
</html>

[CSS not important] [jQuery]

$(document).ready(function(){
            $( "a.btn" ).click(function() {
                var textValue = $('textarea#textareamsg').val();
                if(textValue == "" || textValue == " " || textValue == "  " || textValue == "   "){
                    alert('You have to write something in the textarea to post a message');
                    return false;
                }else{
            $('#content').append("<p>You: " + textValue + "</p>");
            $('textarea#textareamsg').val('');
            }
            });
        });

[AjaxLoad.php]

<?php
//conection:
$link = mysqli_connect("localhost","root","123","chat") or die("Error " . mysqli_error($link));

//consultation:

$query = "SELECT * FROM messages" or die("Error in the consult.." . mysqli_error($link));

//execute the query.

$result = mysqli_query($link, $query);

//display information:

while($row = mysqli_fetch_array($result)) {
  echo '<p class="other">';
  echo $row ["message"] . '</p>';
}
?> 

[AjaxPost]

<?php
$link = mysqli_connect("localhost","root","123","chat") or die("Error " . mysqli_error($link));

//consultation:

$query = "SELECT * FROM messages" or die("Error in the consult.." . mysqli_error($link));

//execute the query.

$result = mysqli_query($link, $query);

//Code goes here

$message = $_POST['messages'];

mysqli_query($link,"INSERT INTO messages (message)
VALUES ('$message')");
?>
  • 写回答

2条回答 默认 最新

  • weixin_33722405 2015-07-13 18:12
    关注

    Use ajax to reload the chat div.

    Steps

    1. Make a chat div
    2. Make  a ajax call every 10 sec
    3. Design a  new page called chat.php
    4. Update the chat data as per the db. 
    

    Moral: call ajax for every 10 sec

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符