dongyi7669 2015-10-09 10: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 11: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**');
          });
    
      });
    
    });
    

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部