2015-10-09 42 views
0

我需要創建圖像的加載DIV而script.sh加載顯示加載圖像在處理Exec_Shell腳本

這裏是我的代碼:

<?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做,但我沒有找到工作的例子

回答

1

使用jQuery方便阿賈克斯。以下代碼假定重新啓動腳本在重新啓動完成時輸出「完成」,而其他任何錯誤都會輸出。

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

    }); 

}); 
+0

按鈕單擊如果你沒有方法的系統得到.SH腳本不返回任何輸出 –

+0

返回完整或失敗的reboo噸,那麼阿賈克斯裝載機將旋轉和旋轉。 除非我誤解了這個問題? –

+0

另一個問題,你是否將shell_exec發送到背景?如果是這樣,服務器重新啓動將繼續和頁面以相同的速度加載,在這種情況下,林不知道我看到一個加載器gif的點。 –