dongyi7669 2015-10-09 18:05
浏览 58
已采纳

处理Exec_Shell脚本时显示加载图像

I need to create a loader div with an image while script.sh is loading

Here's my code:

<?php
    if (isset($_POST['restartserver']))
    {
         shell_exec("my-script.sh");         
    }    
?>

<html>
<body>
<div class="refresh">
<form method="post">
    <p>
        <button class="button" name="restartserver">Restart Server</button>
    </p>
</form>
</div>
</body>
</html>   

So when i click on the button i need a loading image , when the script.sh is loaded the image-loader disapper and you can click again the button in the div.

Actually when i click on the button the script runs correctly but i see that the browser is loading the script, when finished it reload the same page.

I just want a loder image.gif when the script is loading

I know that this can be done with Ajax but i didn't find a working example

  • 写回答

1条回答 默认 最新

  • drsxzut183207938 2015-10-09 19:36
    关注

    Use jQuery for easy ajax. Following code assumes the restart script outputs "complete" when the reboot is done, and anything else for an error.

    $('#loaderdiv').html('<img src="loader.gif" />');
    
    $.get('http://path.to/restart.php', function(data){
        if (data == "complete"){
           $('#loaderdiv').html('Reboot Complete');
        } else {
           $('#loaderdiv').html('An Error Has Occured');
        }
    
    });
    

    HTML

    <div id="container">
        <button id="runscript">Run Script</button>
    </div>
    

    Javascript

    $(document).ready(function(){
    
      $('#runscript').click(function(e){
          e.preventDefault();
    
          $('#container').html('<img src="loader.gif" />');
    
          $.get('http://path.to/execute_sh_script.php', function(data){
                // Code here executes once ajax is complete
                $('#container').html('**strong text**');
          });
    
      });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制