如何使用 Ajax 显示 php 循环的步骤? (长轮询...)

发布于 2024-11-18 16:30:42 字数 1081 浏览 2 评论 0原文

我有一个 PHP 服务器端脚本,在 foreach 循环内,通过 SSH 连接到路由器并执行一些配置块。

    <?php
function apply($hostname){
    $ssh = new Net_SSH2($hostname);
    if (!$ssh->login($this->username, $this->password)) {
       exit('Login Failed');
    }
    $i = 1;
    foreach($NMConf->configuration as $conf_step){
       $ssh->write($conf_step); //Can take 5s...
       $this->ssh_errors[] = $ssh->read("#");
       echo "Step".$i." ok.. <br/>";
       $i++;
       flush();
       sleep(1);}
} ?>

这个函数由 jQuery 和 Ajax 请求调用...

$.ajax({
    url:'apply',
    type:'POST',
    cache:false,
    async:false,
    data:{},
    success: function(data){
      console.log(data);
      $('#myDiv').append(data); // *_*
    }   
 });

我想在我的输出 div 中显示不同的步骤,例如...

Step 1 (working....)

然后

Step 1 OK!
Step 2 (working ....)

然后

Step 1 OK!
Step 2 OK!

我该怎么做?

我查看了长轮询的内容&反向ajax,不过好像有点棘手。

任何想法!

谢谢

I have a PHP server-side script which, inside a foreach loop, connects to a router through SSH and executes some configuration blocks.

    <?php
function apply($hostname){
    $ssh = new Net_SSH2($hostname);
    if (!$ssh->login($this->username, $this->password)) {
       exit('Login Failed');
    }
    $i = 1;
    foreach($NMConf->configuration as $conf_step){
       $ssh->write($conf_step); //Can take 5s...
       $this->ssh_errors[] = $ssh->read("#");
       echo "Step".$i." ok.. <br/>";
       $i++;
       flush();
       sleep(1);}
} ?>

This fonction is called by jQuery and an Ajax request...

$.ajax({
    url:'apply',
    type:'POST',
    cache:false,
    async:false,
    data:{},
    success: function(data){
      console.log(data);
      $('#myDiv').append(data); // *_*
    }   
 });

I would like to display into my output div the different steps, like..

Step 1 (working....)

then

Step 1 OK!
Step 2 (working ....)

and then

Step 1 OK!
Step 2 OK!

How can I do that?

I had a look on long polling stuff & reverse ajax, but it seems a bit tricky.

Any ideas!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2024-11-25 16:30:42

我的建议是,将信息放入会话中,并尝试向 php 脚本询问状态

创建将读取会话的脚本

<?php 
         $step = $_GET["step"];
        if($_SESSION["last_step"] != $step) //just to avoid same info twice
        {
        if(isset($_SESSION['step'.$step])
           echo $_SESSION['step'.$step];
        else
        }
        else { echo '10' }; //this will tell you that next step is not executed jet  
        ?>

更新现有脚本,以在完成或处理会话时将数据放入会话中。
更新您的 JavaScript 以每秒或您想要的时间执行,例如

var _int = setInterval(your_method, 1000);

,完成所有步骤后,只需清除间隔_int clearInterval(_int)

What I propose, info of stuff put in session, and try to ask php script for status

Create script which will read session

<?php 
         $step = $_GET["step"];
        if($_SESSION["last_step"] != $step) //just to avoid same info twice
        {
        if(isset($_SESSION['step'.$step])
           echo $_SESSION['step'.$step];
        else
        }
        else { echo '10' }; //this will tell you that next step is not executed jet  
        ?>

Update your existing script to put data into session when done or working on it.
Update your javascript to execute every second or time you want e.g.

var _int = setInterval(your_method, 1000);

After all steps are done just clear interval _int clearInterval(_int)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文