gpt4 book ai didi

php - 当数据库中有新数据时,使用jquery刷新div不显示新内容

转载 作者:行者123 更新时间:2023-11-29 08:51:53 25 4
gpt4 key购买 nike

我创建了一个 php 文件,其中有一个 div,每 10 秒刷新一次。在此 div 中加载查询结果。演示:http://mauricederegt.nl/test/test.php

看起来像是在刷新div,但是如果数据库中添加了新数据,div不会获取新数据,而是继续显示旧数据。

我使用与需要重新加载的页面相同的页面:

$('#scoreboard-overview').fadeOut('slow').load('index.php #scoreboard-overview').fadeIn("slow");

如果我将 div 中使用的 sql 查询复制到外部 php 文件(例如 reload.php),然后像这样调用它:

$('#scoreboard-overview').fadeOut('slow').load('reload.php').fadeIn("slow");

它可以工作并且 div 显示最新数据。问题是:我不想使用另一个外部 php 文件。

我怎样才能做到这一点?

我还尝试将 $resulttt在div中查询,但这也没有效果。我错过了什么?

亲切的问候,

index.php中使用的代码:

<?php
/////////////////////////////////////////////

//mysql_connect(servername,username,password);
//Here we connect to the database

/////////////////////////////////////////////
//Get the data
$resulttt = mysql_query("SELECT fbID, highscore, stars, MIN(time) AS time FROM highscore GROUP BY fbID, highscore ORDER BY highscore DESC, time ASC LIMIT 7");



?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

<head>
<meta charset="utf-8">
<title>Test</title>
<link rel="stylesheet" href="test.css" />
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>
<script>
var auto_refresh = setInterval(
function()
{
$('#scoreboard-overview').fadeOut('slow').load('index.php #scoreboard-overview').fadeIn("slow");
}, 10000);
</script>
</head>
<body>
<div id="scoreboard">
<div id="scoreboard-title">High scores</div>
<div id="scoreboard-overview">
<?php echo '<ul>';
$num_rows = mysql_num_rows($resulttt);
$i = 0;
while($row = mysql_fetch_array($resulttt)) {
$i = $i + 1;
$fb_data = json_decode(file_get_contents('http://graph.facebook.com/'. $row['fbID']));
$fb_locale_str = $fb_data->locale;
$fb_name_str = $fb_data->first_name;
$fb_country_str = strtolower(substr($fb_locale_str, -2));
$flag_uri = 'images/flags/unknown.gif'; //display a general flag if unknown.
$row['fbnr'] = $row['fbID'];

echo '<li id="'. $row['fbnr'] .'">
<div class="userpicture"><img src="https://graph.facebook.com/'. $row['fbID'] .'/picture" alt="" /></div>
<div class="username"><p>'.$i.'. '.$fb_name_str.'</p></div>
<div class="userflag"><img src="'.$flag_uri.'" alt="" /></div>
<div class="userholder">
<div class="usertime"><p><img src="images/time.png" alt="" /><br>'. $row['time'] .'</p></div>
<div class="userstars"><img src="images/'. $row['stars'] .'s.png" alt="" /></div>
<div class="userscore"><p>'. $row['highscore'] .'</p></div>
</div>
</li>';
}
echo '</ul>'; ?>

</div>
<div id="scoreboard-bottom">end</div>
</div>
</body>
</html>
<小时/>

我想我已经找到了使用 GET 的解决方法,感谢大家的帮助

最佳答案

我无法想象任何浏览器会对 Javascript 尝试将整个 HTML 页面(包含 <html><head><body> 标签)插入现有文档中的元素感到高兴。你想把scoreboard-overview的内容在加载的页面上进入 scoreboard-overview在现有页面上,因此将选择器添加到对 .load() 的调用中:

$('#scoreboard-overview').fadeOut('slow').load('index.php #scoreboard-overview').fadeIn("slow");

关于php - 当数据库中有新数据时,使用jquery刷新div不显示新内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10934639/

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