简体   繁体   English

关于自动刷新的一部分<div>在php文件中

[英]About auto refresh a part of <div> in the php file

I want to test auto refresh in a php file.我想在 php 文件中测试自动刷新。 The following I'd the code.下面是我的代码。 However, I cannot do it.但是,我做不到。 I don't know what is the problem?我不知道是什么问题?

<html>
<head>
<title>TEST</title>
</head>
<body>
<script type="text/javascript">
    window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval("refreshBlock();",1000);
    }

    function refreshBlock()
    {
       $('#test').load("test.php");
    }
  </script>

<div id="test">
    <?php echo rand(); ?>
</div>

</body>
</html>

Do like that.这样做。

<html>
<head>
<title>TEST</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
    window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval(refreshBlock,1000); //Call function like this
    }

    function refreshBlock()
    {
       $('#test').load("test.php");
    }
  </script>

<div id="test">
    <?php echo rand(); ?>
</div>

</body>
</html>

You use jQuery code so please add jQuery library You can get link from here您使用 jQuery 代码,因此请添加 jQuery 库您可以从这里获取链接

https://www.w3schools.com/jquery/jquery_get_started.asp https://www.w3schools.com/jquery/jquery_get_started.asp

This will be working fine这将正常工作

<html>
<head>
  <title>TEST</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
window.onload = setupRefresh;
function setupRefresh()
{
    setInterval(refreshBlock,1000); 
}

function refreshBlock()
{
    $('#test').load("http://localhost/test.php"); // just update your test file url
}
 </script>

 <div id="test">
  <?php echo rand(); ?>
 </div>

 </body>
</html>

This is your test.php file(example code)这是你的 test.php 文件(示例代码)

<?php
 echo date("Y-m-d H:i:s");

?>

Output will be like this and auto update time输出将是这样的,并且自动更新时间

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM