在 Ajax 中为 Div 接收数据
Receiving data in Ajax for a Div
所以这个问题已经让我徒劳地追逐了一个星期左右,我真的希望这个问题最终能够在今晚得到解决。我对 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 脚本时自动启动将滚动条发送到页面的最底部?
ajax看起来不错,因为它已准备好接收数据。在 php 中你可以设置任何你需要的数据,它可以是数据库调用的结果。这是一个将一些数据发送回 ajax 脚本的小示例。
$data = array(
'status' => 'ok',
'message' => 'Customer account saved',
);
return json_encode($data);
如果您知道如何在服务器上获取所需的任何数据,您可以对其进行编码,然后return将其发送给客户端。
成功方法将 运行 在 ajax 对象上。它已传递数据,您可以引用并 manipulate/use 它。您的代码看起来已经为此做好了准备:
success: function(data) { // <-- this is the data in json format from the server
console.log("success");
var $content = $(".list-group-message");
$content.text(data); // Here you have to insert the received data.
所以这个问题已经让我徒劳地追逐了一个星期左右,我真的希望这个问题最终能够在今晚得到解决。我对 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 脚本时自动启动将滚动条发送到页面的最底部?
ajax看起来不错,因为它已准备好接收数据。在 php 中你可以设置任何你需要的数据,它可以是数据库调用的结果。这是一个将一些数据发送回 ajax 脚本的小示例。
$data = array(
'status' => 'ok',
'message' => 'Customer account saved',
);
return json_encode($data);
如果您知道如何在服务器上获取所需的任何数据,您可以对其进行编码,然后return将其发送给客户端。
成功方法将 运行 在 ajax 对象上。它已传递数据,您可以引用并 manipulate/use 它。您的代码看起来已经为此做好了准备:
success: function(data) { // <-- this is the data in json format from the server
console.log("success");
var $content = $(".list-group-message");
$content.text(data); // Here you have to insert the received data.