简体   繁体   English

为什么此PHP AJAX MysQl聊天脚本需要页面刷新?

[英]Why is this PHP AJAX MysQl Chat Script needs page refresh?

I wrote a very simple chat script that inserts data with AJAX to avoid page refresh. 我编写了一个非常简单的聊天脚本,该脚本使用AJAX插入数据以避免页面刷新。 The data inserts but I need to refresh the page in order to see the data that was inserted. 数据已插入,但是我需要刷新页面才能查看已插入的数据。 I use jQuery to avoid page refresh. 我使用jQuery以避免页面刷新。 Can someone help? 有人可以帮忙吗?

script 脚本

$("#submit").click( function() {
 $.post( $("#chatForm").attr("action"), 
         $("#chatForm :input").serializeArray(), 
         function(info){ $("#result").html(info); 
   });
 clearInput();
});

$("#chatForm").submit( function() {
  return false; 
});

function clearInput() {
    $("#chatForm :input").each( function() {
       $(this).val('');
    });
}

form.php form.php

    <form id="chatForm" action="chat.php" method="post">
    <input id='message' name="message" type="text" class="form form-control messageBar" placeholder="Write message here..."/>
    <input id='employee_id' name='employee_id' type="hidden" value="<?=$session_myemployeeid;?>">
    <div class='col-md-2 pull-right'>
    <button id="submit">Send Comment</button>
    </div>
    </form>

chat.php chat.php

<?php
include '../includes/config.php';

// set parameters and execute
$employee_id = $_POST['employee_id'];
$message= $_POST['message'];

// prepare and bind
$insertchat= $db->prepare("INSERT INTO companychatroom (employee_id,message) VALUES (?, ?)");
$insertchat->bind_param("is",$employee_id,$message);
$insertchat->execute() or die(mysqli_error($db)); 

$insertchat->close();
$db->close();

display.php display.php

   <div id="displayMessage" class="displayMessage">
     <?php 
        $sqlchat="SELECT * FROM companychatroom 
        JOIN employees 
        ON companychatroom.employee_id=employees.employee_id";
        $resultchat=  mysqli_query($db,$sqlchat);
       while($chat=mysqli_fetch_array($resultchat)){ ?>
           <div class="row" style="padding:4%;">
               <p><?=$chat['first_name'];?> <?=$chat['last_name'];?></p> <div class="bubble"><?=$chat['message'];?></div>
           </div>
       <?php };?>

       </div>

OK Let's put this together into an answer: 好的,我们将其汇总为一个答案:

Modify the .POST success function to move the result into the page: 修改.POST成功函数以将结果移动到页面中:

$("#submit").click( function() {
 $.post( $("#chatForm").attr("action"), 
         $("#chatForm :input").serializeArray(), 
         function(info){ $("#displayMessage").html(info); 
   });
 clearInput();
});

Modify the chat.php to generate the mark up 修改chat.php以生成标记

<?php
include '../includes/config.php';

// set parameters and execute
$employee_id = $_POST['employee_id'];
$message= $_POST['message'];

// prepare and bind
$insertchat= $db->prepare("INSERT INTO companychatroom (employee_id,message) VALUES (?, ?)");
$insertchat->bind_param("is",$employee_id,$message);
$insertchat->execute() or die(mysqli_error($db)); 
$insertchat->close();

$sqlchat="SELECT * FROM companychatroom 
          JOIN employees 
          ON companychatroom.employee_id=employees.employee_id";
$resultchat=  mysqli_query($db,$sqlchat);
$result = '';
while($chat=mysqli_fetch_array($resultchat)){ 
    $result .= '<div class="row" style="padding:4%;">';
    $result .= '<p>';
    $result .= $chat['first_name'].' '.$chat['last_name'];
    $result .= '</p> <div class="bubble">';
    $result .= $chat['message'];
    $result .= '</div></div>';
}
echo $result;
$db->close();

Change display.php to 将display.php更改为

<div id="displayMessage" class="displayMessage"></div>

DISCLAIMER - this is untested, but applying the principles demonstrated should work for you. 免责声明-这未经测试,但是应用所展示的原理应该对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM