PHP&;Javascript进度条
我试图制作一个进度条,显示长PHP脚本的状态。目前,我有一个代码可以处理给定数量的预设任务,但我希望它能够动态地获取整个脚本的状态 代码如下: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
<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> <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;\"> </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/)