开发者

How can I display steps of my php loop with Ajax? (long polling...)

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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜