简体   繁体   中英

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

I want to test auto refresh in a php file. 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

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)

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

?>

Output will be like this and auto update time

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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