-1

I'm using JQuery in a PHP file to load the files of another content called 'live.php' which displays time(), but it doesn't load live. Why is this happening? It used to work.

MAIN.PHP

    echo "<div id='x'></div>";
    echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">';
    echo '$(document).ready(function() {
            $("#x").load("live.php");
        });';
    echo '</script>';

LIVE.PHP

<?php echo time();

1 Answers1

0

You have missed the closing PHP tag in the LIVE.PHP
It must be like: <?php echo time(); ?>
You have missed the ?> in your PHP file, so the PHP file would not work. So make sure you add ?> to your LIVE.PHP

And for displaying live time without refreshing the page, you can use this jQuery:

$(document).ready(function(){  
        setInterval(function(){   
            $("#x").load("live.php");
        }, 1000);
    });

This updates the page every 1 second.
You can change the 1000 to your desired limit (The 1000 stands for 1000 miliseconds)

  • No, the PHP works fine. It just doesn't load constantly. I have to refresh the page to see the time() change. –  Sep 23 '20 at 05:24
  • I have edited the answer :) See if this helps in anyway! – 404 - Brain Not Found Sep 23 '20 at 05:31
  • Thanks, but the time() was just an example. The real issue was loading a whole PHP file without refreshing. I don't really want to show the time() live. –  Sep 23 '20 at 05:47
  • I guess you can just echo the time() in the main.php file, so that you don't have to refresh the page everytime :) – 404 - Brain Not Found Sep 23 '20 at 06:20
  • I said... time() was an example. I want to load the content from another file LIVE, but the function LOAD() is not working –  Sep 23 '20 at 14:40