AJAX Div使用PHP刷新
我试图每隔一段时间刷新页面上的一些元素。我很清楚,现在在这里一万主题有关,我试图让我的工作,但这里是我需要刷新..AJAX Div使用PHP刷新
这是获取生成的代码加载网页时:
<div id="galleria">
<?php
$a = array();
$dir = '../public/wp-content/uploads/2012/01';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (preg_match("/\.png$/", $file)) $a[] = $file;
elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
}
closedir($handle);
}
$totalImgs = count($a);
$imgUsed = array();
for ($j = 0; $j < 100; $j++)
{
do
{
$randIndex = mt_rand(0, $totalImgs);
}
while ($imgUsed[$randIndex] === TRUE);
$imgUsed[$randIndex] = TRUE;
echo "<img src='" . $dir . '/' . $a[$randIndex] . "' />";
}
?>
</div>
我想每10秒自动刷新一次,但不能重新加载页面。我已经阅读了Ajax,似乎这是可能的,但我似乎无法让它工作。
这一切都是显示广场div,并加载div中的100个图像。然后,环球免税店脚本接管并很好地显示它。 AJAX会更好地工作还是JQuery?
谢谢你的帮助!
使用setInterval
函数与ajax调用。
http://jquery-howto.blogspot.com/2009/04/ajax-update-content-every-x-seconds.html
“将AJAX更好地工作或jQuery的?” - AJAX是一种技术,jQuery是一个库。事实证明,jQuery有一个非常好的AJAX API。
我们称之为PHP“galleria.php”。在原始页面加载时,使用好ol'<?php include('galleria.php')?>
将其插入父级PHP页面。现在最终用户正在看到完整的初始化页面。
要更新它,你必须提供一些AJAX选项,但最简单的就是包括你的页面上的jQuery,然后你可以在脚本中使用:
var updateGallery = setInterval(function() {
$('#someDiv').load('galleria.php');
}, 10000);
有空间调整.. 。也许galleria.php
不包括在页面上设置的<div id="galleria">
。在这种情况下,您将直接加载到#galleria
而不是#someDiv
,并为自己节省一个不必要的容器。也许你缓存$('#someDiv')
对象声明它在一个不同的范围,以便它可以重新使用。但这是一般的要点。
正如我写的here你可以用jQuery ajax调用来填充div。
<html>
<head>
<script type="text/javascript">
function refresh_gallery(){
$.ajax({
type: "POST",
url: "generate_gallery.php", // your PHP generating ONLY the inner DIV code
data: "showimages=100",
success: function(html){
$("#output").html(html);
}
});
}
$(function() {
refresh_gallery(); //first initialize
setTimeout(refresh_gallery(),10000); // refresh every 10 secs
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
完美!谢谢! – 2012-01-12 18:38:12
因此,与外部资源的链接可以作为更好的答案,因为他能够在不到一分钟的时间内尽快发出吱吱声(因为我正在写一些内容)?预计答案是独立的。甚至没有upvote?有时我想放弃堆栈溢出。 :-D – 2012-01-12 18:49:41