我有一个php代码,它是一个div。它显示随机文本放置在random.txt文件中。我想刷新这个div,在每一个 - 比方说 - 加载新文本15秒,而不刷新whoe页面。 我发现了几个解决方案来重新加载div,而不刷新页面,它似乎可以用AJAX或JS完成,但他们只刷新一个特定的内容或特定的文件,但我不知道如何插入这个代码并刷新它。刷新包含PHP代码的div,但不刷新页面
这是我的div的样子:
<div id="randomtext">
<?php
include_once("GetRandomText.php");
$MPTextFile = "random.txt";
$MPSepString = "*divider*";
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>
</div>
它加载每隔更新随机文本到div。
我试图用下面的JS刷新div,但它根本不工作。对不起,我不擅长。
<script>
var auto_refresh = setInterval(
function() {
var newcontent= '<?php
include_once("GetRandomText.php");
$MPTextFile = "random.txt";
$MPSepString = "*divider*";
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>';
$('#randomtext').html(newcontent);
}, 1000);
</script>
<div id="randomtext"></div>
在此先感谢。
谢谢,该脚本工作完美 – takacsbalazs