0

I have a simple script to check status of particular comp on local net, and would like to make a popup prompt box showing status, and asking client if he wants to check again in number of seconds. PHP Code looks something like this:

<!DOCTYPE html>
<html>
<head> <meta charset="UTF-8">
</head>
<body>
<?php
$STS = shell_exec('nc -v -z -w 3 10.1.2.3 3389 &>/dev/null  && echo "Online" || echo "Offline"');
echo "<h5>Server is currently:<b> $STS</h5></b>";
for ($x = 0; $x <= 10; $x++) {
    $STS = shell_exec('nc -v -z -w 3 10.1.2.3 3389 &>/dev/null  && echo "Online" || echo "Offline"');
    if ( strcmp($STS, "Offline") !== 0) {

        function prompt($prompt_msg){
            echo("<script type='text/javascript'> var answer = prompt('".$prompt_msg."');    </script>");
            $answer = "<script type='text/javascript'> document.write(answer)</script>";
            return($answer);
        }

        $prompt_msg = "Currently Offline.  Check Again in how many seconds?";
        $delay = prompt($prompt_msg);
        if ( $delay > 0 ) { 
            sleep ($delay);
        }
    } elseif ( strcmp($STS, "Online" ) == 0) {
        echo "<h5>Server is ready! </h5>";
        $x = 10;
    }
}
?>
</body>
</html>

  

So I would like the pop-up and when clicked OK to run sleep() and check again if cancel it would close and stop. I don't know how to check, if it was clicked Cancel. Also script seems to hang after first round of loop.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
DenisZ
  • 171
  • 1
  • 11
  • 1
    The mentioned duplicate does not directly answer your question, but it explains some basics you seem not to be aware of yet. Your whole attempt at mixing JS & PHP logic here makes little sense to begin with. – CBroe Dec 02 '21 at 12:37
  • Also it is a bad idea defining functions inside an IF, it will catch you out one day when the if has not been entered and you are expecting the function to exist. And worse still is defining a function inside and IF that is inside a LOOP. There almost definitely, be dragons :) – RiggsFolly Dec 02 '21 at 12:43
  • Thanks @CBroe I would ideally display status on the popup, and run javascript there, but didn't find an option how to do that. – DenisZ Dec 02 '21 at 12:58
  • You can not just mix client-side JavaScript and PHP together like this. If you want any value you have available in JS, to be available in PHP - then you must send that JS value to the server by making a new HTTP request. – CBroe Dec 02 '21 at 13:05

0 Answers0