0

I have a value on my PHP page and I want to refresh it per second with setInterval().

So I actually know how to refresh values with html etc. But now I want to do the same with php values. Here is my code:

<script>
setInterval(function()
    {

<?php
$urlMachineOnline = 'http://192.168.0.150/awp/Shredder/PLCfiles/MachineOnline.html';

// get content
$contentMachineOnline = file_get_contents($urlMachineOnline);

//remove first 2 characters
$truncateMachineOnline = substr($contentMachineOnline, 2);

//remove last 5 characters
$MachineActivityMS = substr($truncateMachineOnline, 0, -5);

//Set the value to seconds
$MachineActivityS = floor($MachineActivityMS /1000);

$formatMachineActive = 'H:i:s'; 

$TimeMachineActive = gmdate($formatMachineActive, $MachineActivityS);

?>

},1000);

</script>

Ofc this isn't working since JS and php arent really great together. and in my table I just simply have:

<table>
    <tr>
        <td>Activity:</td>
        <td><p id='MachineActivity'></p><?php echo $TimeMachineActive; ?></td>
    </tr>
</table>

So the problem now is, it's only refreshing when I press f5. But now I want the autorefresh. I know setInterval() worked for html. Is it possible to get this done for php code?

Bart
  • 717
  • 1
  • 9
  • 28
  • You need to write the value to

    using javascript rather than

    – Hammad Mar 18 '15 at 10:50
  • Ofc this isn't working, JS doesn't bother PhP, it's just you didn't wrote correclty your JS. – Anwar Mar 18 '15 at 10:50
  • Preferred way of doing so to use Ajax rather than mingling JS and PHP code. – Hammad Mar 18 '15 at 10:51
  • @Hammad I can't mix JS and php values with eachother can I? – Bart Mar 18 '15 at 10:53
  • In fact, what you've done result in some random text, inside your `SetInterval` function. You should store the result of PhP in several JS variable using `var myVar = ""` – Anwar Mar 18 '15 at 10:54
  • Yes you can do that but better not do that. First write a ajax function in setInterval method calling $urlMachineOnline. Whatever is the result from that ajax response is write that value to

    . In this way your value will be refreshed after 1 second.

    – Hammad Mar 18 '15 at 10:55

6 Answers6

2

This should work for you:

JS Code:

<script>
setInterval(function()
{

    $.ajax({

    url: 'value-generation.php',
    type: 'get',
    success: function(response){

    $("#MachineActivity").html(response)
    },

    });

        },1000);

    </script>

value-generation.php code:

<?php
$urlMachineOnline = 'http://192.168.0.150/awp/Shredder/PLCfiles/MachineOnline.html';

// get content
$contentMachineOnline = file_get_contents($urlMachineOnline);

//remove first 2 characters
$truncateMachineOnline = substr($contentMachineOnline, 2);

//remove last 5 characters
$MachineActivityMS = substr($truncateMachineOnline, 0, -5);

//Set the value to seconds
$MachineActivityS = floor($MachineActivityMS /1000);

$formatMachineActive = 'H:i:s'; 

$TimeMachineActive = gmdate($formatMachineActive, $MachineActivityS);

echo $TimeMachineActive;

?>
Hammad
  • 2,097
  • 3
  • 26
  • 45
  • It's not working. But it doesnt show any error when I inspect it. – Bart Mar 18 '15 at 12:00
  • Please make sure you include jquery library in you html document.Also value-generation.php must be placed in same directory in which your file running script tag is placed – Hammad Mar 18 '15 at 12:08
  • I've got the 2.1.3 jquery included. What do you mean with the file running script tag? Both files are in the same directory map (if that is what you mean). – Bart Mar 18 '15 at 12:19
  • replace `$("#MachineActivity").next().html(response)` with `$('#MachineActivity').html(response)` and it works like I want it. just counting up the value per second :) – Bart Mar 18 '15 at 12:32
  • If you change your answer with what I said above. I will give you the correct answer mark. – Bart Mar 18 '15 at 12:58
  • Thats good...I said so because there is slight error in your markup. Anyways if its working for you can consider accepting the answer. :) – Hammad Mar 18 '15 at 12:58
1

This is how you convert php value to javascript value

<script>
setInterval(function(){
<?php
$urlMachineOnline = 'http://192.168.0.150/awp/Shredder/PLCfiles/MachineOnline.html';
// get content
$contentMachineOnline = file_get_contents($urlMachineOnline);
//remove first 2 characters
$truncateMachineOnline = substr($contentMachineOnline, 2);
//remove last 5 characters
$MachineActivityMS = substr($truncateMachineOnline, 0, -5);
//Set the value to seconds
$MachineActivityS = floor($MachineActivityMS /1000);
$formatMachineActive = 'H:i:s'; 
$TimeMachineActive = gmdate($formatMachineActive, $MachineActivityS);
?>
var n_val = "<?php echo $TimeMachineActive; ?>";
    console.log(n_val);
},1000);

</script>

Change console and give it to your desire.


But does this make the loading time more ? Every second you are calling a remote page and checking ?

itssajan
  • 820
  • 8
  • 24
0

When you refresh, PHP returns the whole page again, and it cannot refresh parts of the page. So if you want just part of the page refreshed, you'll need to use iframes.

<body>
  <h1>This is my main PHP page</h1>
  <iframe src="[url-to-another-php-page-with-only-the-timer]"></iframe>
</body>

And then you'll have to do a separate php page with just the timer value, and serve the html with a meta tag - this meta tag will do the refresh. Meta tag is detailed in this ticket: PHP - auto refreshing page

Community
  • 1
  • 1
Mihaly KR
  • 2,363
  • 2
  • 19
  • 20
  • When I use this I get another *field* in the topleft of my screen and it shows the page. and it kinda freaking out since it loads a new page in it again again again again and again... :P so you get something like setting 2 mirrors in front of each other. – Bart Mar 18 '15 at 11:53
0

You need to define setInterval function.

function setInterval($f, $milliseconds)
{
    $seconds=(int)$milliseconds/1000;
    while(true)
    {
        $f();
        sleep($seconds);
    }
}

Now call your set interval function and it should work fine.

Asit
  • 458
  • 4
  • 14
  • Yea it kinda needs to auto refresh... not by a button or something – Bart Mar 18 '15 at 11:40
  • You don't need to add button. Here while condition in always true, so it will run infinitely. – Asit Mar 18 '15 at 11:51
  • And how do I use this code, whats should I set in the $f();? the $seconds will just be 1 second... so you dont have to add the MS > second conversion – Bart Mar 18 '15 at 12:07
  • If I remove the 2 parameters and create a function of the php code it crashes. Since it is just infinite looping. This doesn't work. – Bart Mar 18 '15 at 12:19
0

As you know, a PHP file will make the server generate a page when you call its URI. The way you are using your script won't make the server "regenerate" the page and update the values.

Assuming this, you can:

  • Externalize the php code which is in your setInterval function (ex: update_time_machine.php)

  • Recall the externalized PHP resource(using IFrame or a request)

  • Update your page through Jquery/JAVASCRIPT using the PHP script output.

Edit: Mihali's answer sounds the cleanest one.

Answers_Seeker
  • 468
  • 4
  • 11
0

I'm sure this will work for you

 <script>
     setTimeout(function(){
         location.reload();
      },1000); // 1000 milliseconds means 1 seconds.
 </script>
Juri Noga
  • 4,363
  • 7
  • 38
  • 51
  • The problem is **refresh every second** and `setTimeout()` runs only once. – Calos Jan 31 '20 at 08:35
  • Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See also Explaining entirely code-based answers: https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers – borchvm Jan 31 '20 at 08:40
  • @Calos and after the reload it get's called again. – Sebastian Speitel Jan 31 '20 at 08:49
  • @SebastianSpeitel mentioned in the title **Refreshing part of my php page**, which means that just need refresh some content, not completely reloaded. – Calos Jan 31 '20 at 08:56