<div id="txtHint"><img src=bar.gif height=10 width=10></div>
The width we have kept here is 10 but on each stage we will increase the width to 20, 40, 60 like that till it reaches the value 200.
document.getElementById("p1").value= width;
This is the line of code used to display progress tag.
<progress value="0" max="200" id=p1>50%</progress>
function timer(){
ajaxFunction();
if(myForm.width.value<200){
setTimeout('timer()',1000);
}
}
In this code we will be calling our ajaxFunction() at 10 sec interval as long as the width is less than 200.
<?Php
$width=$_POST['width'];
$width=$width+20;
echo $width;
?>
In our form we will keep one hidden field to store the width value. Here is our form part.
<form name="myForm" onsubmit="timer(); return false">
<input type=hidden name=width value=20>
<input type=submit value=Start>
</form>
<div id="txtHint"><img src=images/bar.gif height=10 width=10></div>
<br><br>
<progress value="0" max="200" id=p1>50%</progress>
Here is our full code of progress-bar-demo.php file
<html>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateChanged()
{
if(httpxml.readyState==4)
{
var width=httpxml.responseText;
myForm.width.value=width;
document.getElementById("txtHint").innerHTML="<img src=images/bar.gif height=10 width="+width+"><br>width="+width;
document.getElementById("p1").value= width;
}
}
function getFormData(myForm) {
var myParameters = new Array();
for (var i=0 ; i < myForm.elements.length; i++) {
var sParam = encodeURIComponent(myForm.elements[i].name);
sParam += "=";
sParam += encodeURIComponent(myForm.elements[i].value);
myParameters.push(sParam);
}
return myParameters.join("&");
}
var url="progress-bar.php";
var myForm = document.forms[0];
var parameters=getFormData(myForm);
//alert(parameters);
httpxml.onreadystatechange=stateChanged;
httpxml.open("POST", url, true)
httpxml.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
httpxml.send(parameters)
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
}
/////////////////////////
function timer(){
ajaxFunction();
if(myForm.width.value<180){
setTimeout('timer()',1000);
}
}
</script>
</body>
<form name="myForm" onsubmit="timer(); return false">
<input type=hidden name=width value=20>
<input type=submit value=Start>
</form>
<div id="txtHint"><img src=images/bar.gif height=10 width=10></div>
<br><br>
<progress value="0" max="200" id=p1>50%</progress>
</body>
</html>
Same way here is our progressbar.php file.
<?Php
$width=$_POST['width'];
$width=$width+20;
echo $width;
?>
The progress bar displayed here is not reflection of actual script execution process. Read more on a simulated situation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| shaheb | 14-09-2013 |
| Thanks for create this type of web site. | |