weixin_33736048 2017-10-31 15:48 采纳率: 0%
浏览 29

用户之间的聊天系统

I have been trying to create a chatting system using php+ ajax + mysql.

<?php
  session_start();
?>
<html>  
  <head>  
       <title>Live Table Data Edit</title>  
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
       <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
  </head>  
  <body>  
       <div class="container">  
            <br />  
            <br />  
            <br />  
            <div class="table-responsive">  
                 <h3 align="center">You Are : <?php echo $_SESSION['name']; 
                ?></h3><br />  
                 <div id="live_data"></div>                 
            </div>  
            <div id="messages"></div> 
            <div class="area" style="display:none">
            <textarea id="text" name="text"></textarea>
            <input type="submit" id="sub" name="sub" value="Send" />
            </div>
       </div>  

  </body>  
  </html>  

   <script>  
$(document).ready(function() {
  function fetch_data() {
    $.ajax({
      url: "select.php",
      method: "POST",
      success: function(data) {
        $('#live_data').html(data);
      }
    });
  }
  fetch_data();

  $(document).on('click', '.first_name', function() {
    var id = $(this).data("id1");

    function fetch_chat() {
      $.ajax({
        url: "fetch_chat.php",
        method: "POST",
        data: {
          id: id
        },
        dataType: "text",
        success: function(data) {
          $('#messages').html(data);
          $("div.area").show();
        }

      });
    }

    fetch_chat();
    $("#sub").click(function() {
      var text = $("#text").val();
      $.post('insert_chat.php', {
        id: id,
        msg: text
      }, function(data) {
        $("#messages").append(data);
        $("#text").val('');

      });
      alert(text);
    });
  });
});

but the trouble is that when I try to insert a new message in mysql the messages is send to all the users in which i have clicked.

the fetch_chat.php

<html>
<head>
</head>
<body>
<?php
session_start();
$con=mysqli_connect('localhost','root','','test');
$id= $_POST['id'];
$sql="select * from users where id='$id'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_array($res);
$user_to= $row['name'];
$sql1="select * from chats where user_from='$_SESSION[name]' AND 
user_to='$user_to' OR user_to='$_SESSION[name]' AND user_from='$user_to' 
order  by id";
$res1=mysqli_query($con,$sql1);
if(mysqli_num_rows($res1)>0){
    while($row=mysqli_fetch_array($res1)){
?>
        <b> <?php echo $row['user_from']; ?>:</b> 
        <?php echo $row['msg']; ?><br /><br />
        <?php
    }
}
else
    echo "No msgs <br />";
?>

</body>
</html>

and insert_chat.php is:

<html>
<head>
</head>
<body>
<?php
session_start();
$con=mysqli_connect('localhost','root','','test');
$id= $_POST['id'];
$msg=$_POST['msg'];
$sql="select * from users where id='$id'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_array($res);
$user_to= $row['name'];

$sql1="insert into chats(user_from,user_to,msg) 
values('$_SESSION[name]','$user_to','$msg')";
$res1=mysqli_query($con,$sql1);
if($res1){
?>
    <b> <?php echo $_SESSION['name']; ?>:</b> 
    <?php echo $msg; ?><br /><br />
<?php   
}
?>
</body>
</html>

I don't know where I am going wrong. Please help me with this.

For example, the list of users is:

(1) user a

(2) user b

(3) user c

So at first I clicked a then I changed my mind to select b and send the text. But there arises the problem: the data gets inserted for both a and b like I am texting both of them at the same time.

  • 写回答

1条回答 默认 最新

  • ?Briella 2017-10-31 16:43
    关注

    You keep on adding more and more "click" event handlers to "sub", every time you click on "first_name". So then each time you click sub again, it runs all the previous event handlers again and again. So you get the current message, but sent to all users you previously clicked on since you loaded the page.

    $("#sub").click(function() { 
    

    needs to be

    $("#sub").off("click").on("click", function() { 
    

    so that it removes previous handlers before you define the new version with new data.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么