gpt4 book ai didi

php - AJAX 设置间隔

转载 作者:行者123 更新时间:2023-12-01 04:47:58 25 4
gpt4 key购买 nike

我想获取表中最新的post_id而不刷新它,但问题是每当用户向数据库插入一个值时,它就会无限回显最后一个post_id。我希望它只回显一次。但我仍然想从表中获取最新的post_id。

这是我的主要 php:

<div id = "this_div">
<?php
include 'connect.php';
session_start();
$query = "SELECT post_id FROM tbl_posts ORDER BY post_id ASC LIMIT 20";
$execute_query = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($execute_query))
{
$get_this_id = $row['post_id'];
echo $get_this_id."<br>";
}
$_SESSION['get_this_id'] = $get_this_id;
?>
</div>

这是我的 jQuery ajax:

 <script>
var refreshId = setInterval(function(){
compare_session = "<?php echo $_SESSION['get_this_id']; ?>";
$.ajax({
url: 'another_file.php',
data: {},
success: function(data)
{
if(compare_session != data)
{
$('#this_div').text($('#this_div').text()+data);
}
}
});
},400);
</script>

这是 another_file.php 的 php 代码

<?php
include 'connect.php';
session_start();
$query = "SELECT post_id FROM tbl_posts ORDER BY post_id DESC LIMIT 1";
$execute_query = mysqli_query($con,$query);
if($row = mysqli_fetch_assoc($execute_query))
{
echo $get_this_id = $row['post_id'];
}
?>

最佳答案

您没有更新 compare_session 变量,它始终保留初始值。因此在成功回调函数中更新它

compare_session = "<?php echo $_SESSION['get_this_id']; ?>";
var refreshId = setInterval(function () {
$.ajax({
url: 'another_file.php',
data: {},
success: function (data) {
if (compare_session != data) {
$('#this_div').text($('#this_div').text() + data);
}
compare_session = data;
}
});
}, 400);

关于php - AJAX 设置间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26325990/

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