如何在 PHP 执行批处理文件时显示加载图像 gif 或消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6292952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a loading image gif or message while PHP is executing a batch file?
提问by Big Beetle Fan
I've been searching on the net for the answer, but couldn't find.
我一直在网上寻找答案,但找不到。
How could I show some loading messsage or gif while the long executing script is running. I tested a number of different way like javascript, because my script is trying to use PLINK.exe to tail a file, which take about 30s to return the value. because it's one line code, I can't use flush(), is there any other way I cam make this happen?
我怎么能在长时间执行的脚本运行时显示一些加载消息或 gif。我测试了许多不同的方式,比如 javascript,因为我的脚本试图使用 PLINK.exe 来拖尾文件,这需要大约 30 秒才能返回值。因为它是一行代码,我不能使用flush(),还有其他方法可以实现吗?
<?php
$runCommand = "C:\wamp\www\TS\batch\plink.exe Sever -l User \"ssh User@Server 'tail -600 /serverlog/test.log'\" ";
$results=system($runCommand);
//exec($runCommand, $results);
echo $results;
?>
回答by Big Beetle Fan
I've tried the following code, which given me exactly what I want. the page is displaying a "loading.gif" while loading the php script, and hide it when the script is finished. This is using JQuery.
我试过下面的代码,它给了我我想要的。页面在加载 php 脚本时显示“loading.gif”,并在脚本完成后隐藏它。这是使用 JQuery。
//$tr_arname=$_REQUEST['arname'] -> is the variable i've got from previous PHP page.
<body onload="loadingAjax('myDiv');">
<script>
var arname="<?php $tr_arname=$_REQUEST['arname']; echo "$tr_arname"; ?>";
function loadingAjax(div_id)
{
$("#"+div_id).html('<center><img src="images/loading.gif"><br><br><font color="#006699" face="arial" size="4"><b>Loading arerror.log <br><?php echo "$tr_arname"; ?> <br>Please Wait ...</b></font></center>');
$.ajax({
type: "POST",
url: "ThePHPScriptPage.php",
data: "arname=" + arname,
success: function(msg){
$("#"+div_id).html(msg);
}
});
}
</script>
<div id="myDiv"></div>
</body>
回答by Jon Skarpeteig
You can request your php file from another web page using AJAX. Then you have your javascript display a loading.gif until the server answers the request.
您可以使用 AJAX 从另一个网页请求您的 php 文件。然后你让你的 javascript 显示一个 loading.gif,直到服务器响应请求。
You could use jQuery for this: http://api.jquery.com/load/
您可以为此使用 jQuery:http: //api.jquery.com/load/
回答by VAShhh
You can't do this using PHP only since is a server-side execution (the page / scripts are rendered in the server before the user can see anything on his browser)
您不能仅使用 PHP 执行此操作,因为它是服务器端执行(页面/脚本在用户可以在其浏览器上看到任何内容之前在服务器中呈现)
You should put your script in a separate file, and then do an AJAX call.
您应该将脚本放在一个单独的文件中,然后执行 AJAX 调用。
To display the loading image follow this (jQuery framework needed)
要显示加载图像,请遵循此(需要 jQuery 框架)
http://jquery-howto.blogspot.com/2009/04/display-loading-gif-image-while-loading.html
http://jquery-howto.blogspot.com/2009/04/display-loading-gif-image-while-loading.html
// SHOW YOUR LOADING GIF HERE
$('#result').load('yourscript.php', function() {
//HIDE YOUR LODAING GIF HERE
});