dongsou8980 2012-05-15 18:29
浏览 31
已采纳

PHP while循环,使用ajax将信息更新到页面

I have a WOL PHP script I am working on, and I was hoping to find a way to refresh part of the page using ajax, or if I should call an external php script?

My PHP script starts a pc and then checks multiple times to see if it is up and running.... However, with PHP the user obviously has to wait until it is all the way done before it displays the results, I was hoping there was a way to make the "while loop" I am running run and update after the first part of the page has loaded?

Essentially, the checkstatus function checks to see if a port is running, then displays output.. I was hoping to load the main page, then update the status of the portcheck using ajax, or another php script? Not sure how.

function CheckStatus($tries, $device, $deviceip, $deviceport, $devicemac1, $devicemac2, $maxtries, $broadcast, $udport, $timeout){
    while($tries <= $maxtries){
        echo "<br \>";
        echo "Try $tries of $maxtries";
        echo "<br \>";
        //echo portcheck($deviceip, $deviceport);
        if (portcheck($deviceip, $deviceport) != 0){
                echo "  ------>  Could NOT open connection to $device at $deviceip on port $deviceport";
                echo "<br \>";                
                //echo "<br /> TRIES: $tries";

            }else{
                echo "<br \>";
                echo "<br \>";
                echo "<br \>";
                echo "TESTED OKAY!  $device";
                return TRUE;
            }
            $tries=$tries + 1;
    }
    echo "<br />";
    echo "MAX TRIES REACHED... COULD NOT CONNECT!";
    return FALSE;
}   
  • 写回答

1条回答 默认 最新

  • doujiu9307 2012-05-15 19:13
    关注

    Having posted a comment saying "You can't do it in a single instance of PHP" I will now go ahead and prove myself wrong.

    It is possible to do this using a sort of server push setup. Consider the following code:

    <?php
    
      // Do all your preliminary stuff here
    
    ?>
    <html>
      <head>
        <title>Server push demo</title>
        <script type="text/javascript">
          function updateTries(tries) {
            document.getElementById('num_tries').innerHTML = tries;
          }
          function pushEvent(str) {
            var event = document.createElement('div');
            event.innerHTML = str;
            document.getElementById('stream_container').appendChild(event);
          }
        </script>
      </head>
      <body>
        <div>
          Number of tries:
          <span id="num_tries"></span>
        </div>
        <div id="stream_container"></div>
    <?php
    
      // Push all output so far to the client
      flush();
    
      // Start status check loop
      CheckStatus($tries, $device, $deviceip, $deviceport, $devicemac1, $devicemac2, $maxtries, $broadcast, $udport, $timeout);
    
      function CheckStatus($tries, $device, $deviceip, $deviceport, $devicemac1, $devicemac2, $maxtries, $broadcast, $udport, $timeout){
    
        while ($tries < $maxtries) {
    
            echo "<script type='text/javascript'>updateTries($tries);</script>";
            flush();
    
            if (portcheck($deviceip, $deviceport) != 0){
    
              echo "<script type='text/javascript'>pushEvent('Could NOT open connection to $device at $deviceip on port $deviceport');</script>";
              flush();
    
            } else {
    
              echo "<script type='text/javascript'>pushEvent('TESTED OKAY! $device');</script>";
              flush();
              return TRUE;
    
            }
    
            $tries++;
    
        }
    
        echo "<script type='text/javascript'>pushEvent('MAX TRIES REACHED... COULD NOT CONNECT!');</script>";
        flush();
        return FALSE;
    
      }
    
    ?>
    </body></html>
    

    However, I still maintain it is better to abstract the loop to the client side and use AJAX to poll the server, which performs one check at a time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!