我的问题是使用 jQuery 自动刷新 div,但使用以下脚本来保持记录跟踪,所以如果用户单击 Load More 我会加载帖子,但 jQuery 刷新 div 的那一刻,它会重置为默认值,知道如何保留加载记录的数量
这是我尝试过的
if(isset($_POST['load_more'])) {
$_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
$fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
} else {
$counter = 10;
$_SESSION['total_counter'] = $counter;
$fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
}
刷新时会发生什么,它使用该$counter
值并忘记加载更多会话值
详细代码
<?php
require_once 'connection.php';
require_once 'functions.php';
if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false) {
header('Location: login.php');
exit;
}
date_default_timezone_set("Asia/Calcutta");
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(".toggle_container").show();
jQuery("div.question_trigger").click(function(){
jQuery(this).toggleClass("active").next().toggle("slow");
});
});
</script>
<script type="text/javascript">
var auto_refresh = setInterval(function() {
$('.messages').load('chat.php .messages', function(){
$(this).fadeIn("fast")
});
}, 2000);
</script>
</head>
<body>
<?php
require_once 'header.php';
if($_SESSION['user_id'] == 1) {
$clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_first = 'yes' WHERE checked_first = ''");
} elseif ($_SESSION['user_id'] == 2) {
$clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_second = 'yes' WHERE checked_second = ''");
} elseif ($_SESSION['user_id'] == 3) {
$clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_third = 'yes' WHERE checked_third = ''");
}
?>
<div class="container">
<form method="POST">
<textarea name="messaging"></textarea>
<input type="submit" value="Post" name="message_input" class="gen_button" />
</form>
<img src="smileys/smile.gif" /><a target="_blank" href="smileys_info.php"> Smileys Info</a>
<br /><br />
<?php
if(isset($_POST['message_input'])) {
$throw_error = array();
if(isset($_POST['messaging']) && $_POST['messaging'] == '') {
$throw_error['msg_blank'] = "Message Cannot Be Blank";
}
if(empty($throw_error)) {
$message = nl2br(htmlspecialchars(mysqli_real_escape_string($connection, $_POST['messaging'])));
$date_time = date("Y-m-d H:i:s");
if(!mysqli_query($connection, "INSERT INTO tbl_chat (msg_from, message, record_date) VALUES('".$_SESSION['user_id']."', '$message', '$date_time')")) {
echo mysqli_error($connection);
}
if($_SESSION['user_id'] == 1) {
if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_first) VALUES('".$_SESSION['user_id']."', 'yes')")) {
echo mysqli_error($connection);
}
} elseif ($_SESSION['user_id'] == 2) {
if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_second) VALUES('".$_SESSION['user_id']."', 'yes')")) {
echo mysqli_error($connection);
}
} elseif ($_SESSION['user_id'] == 3) {
if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_third) VALUES('".$_SESSION['user_id']."', 'yes')")) {
echo mysqli_error($connection);
}
}
else {
header('Location: chat.php');
exit;
}
}
if(!empty($throw_error)) {
if(isset($throw_error['msg_blank'])) {
echo "<span class=\"red\">".$throw_error['msg_blank']."</span>";
}
}
}
if(isset($_POST['load_more'])) {
$_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
$fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
} else {
$counter = 10;
$_SESSION['total_counter'] = $counter;
$fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
}
?>
<div class="messages">
<?php
while($throw_messages = mysqli_fetch_array($fetch_messages)) {
?>
<div class="message_container">
<div class="display_pic">
<img src="<?php echo $throw_messages['user_pic']; ?>" />
</div>
<div class="user_name">
<?php
echo $throw_messages['real_name'];
?>
</div>
<div class="message"><?php smilies($throw_messages['message']); ?></div>
<div class="msg_date_time"><?php echo explode_date_time($throw_messages['record_date']); ?></div>
</div>
<?php } ?>
</div>
<?php
if(mysqli_num_rows($fetch_messages) > 9) {
?>
<form method="POST">
<input type="submit" value="Load More" name="load_more" />
</form>
<?php
}
?>
</div>
</body>
</html>