Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP&;Javascript进度条_Javascript_Php_Loading - Fatal编程技术网

PHP&;Javascript进度条

PHP&;Javascript进度条,javascript,php,loading,Javascript,Php,Loading,我试图制作一个进度条,显示长PHP脚本的状态。目前,我有一个代码可以处理给定数量的预设任务,但我希望它能够动态地获取整个脚本的状态 代码如下: <html lang="en"> <head> <title>Progress Bar</title> </head> <body> <!-- Progress bar holder --> <div id="progress" style="width:500px

我试图制作一个进度条,显示长PHP脚本的状态。目前,我有一个代码可以处理给定数量的预设任务,但我希望它能够动态地获取整个脚本的状态

代码如下:

<html lang="en">
<head>
<title>Progress Bar</title>
</head>
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc; display: inline-block;"></div> &nbsp; <div style="display: inline-block;" id="somethn"></div> <br>
<!-- Progress information -->
<div id="information" style="width"></div>

<?php
// Total processes
$total = 3000;
// Loop through process
for($i=1; $i<=$total; $i++){
// Calculate the percentation
$percent = intval($i/$total * 100)."%";

// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
document.getElementById("somethn").innerHTML="'.$percent.'";
document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
</script>';

// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);

// Send output to browser immediately
flush();
}
// Tell user that the process is completed
// echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>

</body>
</html>

进度条


这样的事情怎么可能实现呢?

那是行不通的。您必须在JS中使用AJAX

这些可能会帮助您:

  • …或更容易使用jQuery:
  • (=>jsfiddle:r86gM/9/)

您无法使用PHP。PHP在完成之前不会输出到浏览器。您要么需要一个后台任务,要么需要单独的Ajax调用,一个用于运行函数,另一个用于跟踪进度的函数。@Devon我听说也可以使用Javascript。这是真的吗?:)如果它是加载到浏览器中的独立页面,则不会。浏览器无法知道哪个服务器正在运行to@user3649604是的,AJAX调用将通过Javascript进行。您可以查看像JQuery这样的库来简化事情。@Devon是的,HTML5可以使用。例如: