How to refresh a page with PHP script using Javascript

How to update echo using javascript? I have something like this:

<?php
$today = time();  
$todayStr = date('D');
$summerS = mktime(0,0,0,2,0,date('Y'));
$summerE = mktime(0,0,0,12,21,date('Y'));
$close = mktime(17,0,0,date('n'),date('j'),date('Y'));
if ($today >= $summerS && $today <= $summerE)
{
    if ($todayStr == "Sat")
    {
        $open = mktime(8,0,0,date('n'),date('j'),date('Y'));
        if ($today >= $open && $today <= $close)
        {          
            $openT = "Otevřeno";
        }
        else
        {
            $nextO = date("H:i:s",($open - $today));
            $openT = "Zavřeno";
        }
    }
    elseif ($todayStr == "Sun")
    {
        $open = mktime(8,0,0,date('n'),date('j'),date('Y'));
        if ($today >= $open && $today <= $close)
        {          
            $openT = "otevřeno";
        }
        else
        {
            $nextO = date("H:i:s",($open - $today));
            $openT = "zavřeno";
        }
    }...

<p>Právě je <?php echo $openT ?></p>

      

And I don't even know how to update this echo in the <p> to display the time for the second.

+3


source to share


1 answer


You cannot "refresh" the echo with javascript as the echo code is executed on the server and the javascript is executed in the browser.

Your two best options:



  • Do all the calculations you do in your PHP client side. Javascript
  • Create a web service that returns $ openT and will call that endpoint using AJAX in the browser.
+3


source







All Articles