【发布时间】:2015-12-13 17:47:22
【问题描述】:
所以这个问题已经让我苦苦追寻了一个星期左右,我真的希望这个问题最终能够在今晚得到解决。我对 Ajax 或 JS 一点经验都没有,所以我在这里真的很挣扎并且仍在学习。这是我希望实现的目标......
我在messages.php 中有一个基本的 PHP 消息传递系统,在 DIV 中显示两个用户之间的所有消息,当您收到更多消息时,它会自动添加一个滚动条。这是执行此操作的 DIV:
<div class="list-group-message" style="overflow-y: scroll;height:385px;width:680px">
<div id="content">
/// PHP MESSAGE SCRIPT
</div>
</div>
当您发送回复时,它会使用此 Ajax 脚本发送要在 system/reply_system.php 上处理的数据,如果它注意到您正在与自动帐户交谈,它也会将数据发送到 system/sars_system.php 以进行处理,这适用于添加和发回消息...
<script>
setInterval(function() {
$("#content").load(location.href+" #content","");
}, 5000);
</script>
<script>
function loadDoc() {
$.ajax({
url: 'system/reply_system.php',
type: 'POST',
dataType: 'json',
data: $('#reply').serialize(),
success: function(data) {
console.log("success");
var $content = $(".list-group-message");
$content.text(data); // Here you have to insert the received data.
$content[0].scrollTop = $content[0].scrollHeight;
// Second ajax
$.ajax({
url: 'system/sars_system.php',
type: 'POST',
dataType: 'json',
data: $('#reply').serialize(),
success: function(data) {
$content.text(data); // Here you have to insert the received data.
$content[0].scrollTop = $content[0].scrollHeight;
},
error: function(e) {
//called when there is an error
console.log('fail');
}
});
},
error: function(e) {
//called when there is an error
console.log('fail');
}
});
}
</script>
帮助我编写此脚本的好心人告诉我,我需要从system/sars_system.php 和system/reply_system.php 接收数据,它们基本上如下所示:
<?
require 'db.php';
$message = $_POST['message'];
$conversation_id = $_POST['conversation_id'];
$sarssystem = $_POST['sarssystem'];
$user_id = $_POST['user_id'];
$usr_message = str_replace("'","\\'",$message);
mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)
VALUES ('','$usr_message','$user_id', NOW(), NOW(), '$conversation_id')");
mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");
echo json_encode('success');
?>
但是我遇到了一个真正的大问题,试图弄清楚如何做到这一点,或者我什至需要发回哪些数据,或者我如何将其编码到当前脚本中?如果这一切正常,最终目标是每次运行此 Ajax 脚本时自动启动将滚动条发送到页面的最底部?
【问题讨论】:
标签: javascript php html ajax messages