gpt4 book ai didi

javascript - 为什么这个 PHP AJAX MysQl 聊天脚本需要页面刷新?

转载 作者:行者123 更新时间:2023-11-29 10:53:40 24 4
gpt4 key购买 nike

我编写了一个非常简单的聊天脚本,它使用 AJAX 插入数据以避免页面刷新。数据插入,但我需要刷新页面才能查看插入的数据。我使用 jQuery 来避免页面刷新。有人可以帮忙吗?

脚本

$("#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 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

<?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

   <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>

最佳答案

好的,让我们将其组合成一个答案:

修改.POST成功函数,将结果移至页面中:

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

修改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();

将display.php更改为

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

免责声明 - 这未经测试,但应用所演示的原则应该对您有用。

关于javascript - 为什么这个 PHP AJAX MysQl 聊天脚本需要页面刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43334998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com