【问题标题】:Show Loading Image while processing Exec_Shell Script在处理 Exec_Shell 脚本时显示加载图像
【发布时间】:2015-10-09 18:05:35
【问题描述】:

我需要在 script.sh 加载时创建一个带有图像的加载器 div

这是我的代码:

<?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>   

所以当我点击按钮时,我需要加载图片,当加载 script.sh 时,图片加载器会消失,您可以再次点击 div 中的按钮。

实际上,当我单击按钮时,脚本运行正常,但我看到浏览器正在加载脚本,完成后它会重新加载同一页面。

脚本加载时我只想要一个加载器 image.gif

我知道这可以通过 Ajax 完成,但我没有找到一个可行的示例

【问题讨论】:

    标签: php jquery html css ajax


    【解决方案1】:

    使用 jQuery 轻松实现 ajax。以下代码假定重启完成后重启脚本输出“完成”,以及其他任何错误。

    $('#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**');
          });
    
      });
    
    });
    

    【讨论】:

    • on 按钮单击系统获取不返回任何输出的 .SH 脚本
    • 如果您无法在重新启动时返回完成或失败,那么 ajax 加载程序将不停地旋转。除非我误解了这个问题?
    • 另一个问题,你是把shell_exec 发送到后台吗?如果是这样,服务器将继续重新启动并且页面以相同的速度加载,在这种情况下,我不确定我是否看到了加载器 gif 的意义。
    • 实际上,当我单击按钮时,脚本 .sh 也会运行(这是一个修改服务器上一些数据的 bash 脚本)。浏览器检测到脚本的完成..脚本运行时我需要一个loader.gif..完成后再次返回按钮
    • 不,shell_exec 没有在后台运行 ..这就是为什么浏览器会检测到它何时完成
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多