Progress Bar using Ajax

If you have studied the Ajax basic tutorials then this is not so difficult to understand. We will try to develop a progress bar which takes the inputs from a php file ( server side script ) and display the result.

Here we will use two files, one is out html file with Ajax and JavaScript code and the other file is server side PHP script. The server side php file will only receive one data and it will increase the value by 20 and return the same value back to the html file.

DEMO of Progress Bar

Displaying the progress bar

To display a progressing bar we will use one small image of fixed height and width. The width we will change ( or increase ) based on the data we receive from the php file by Ajax. This is the code we will keep inside the div tag to display the progress bar.
<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.

Displaying progress by using progress tag ( HTML5)

In HTML5 we have progress tag. The value attributes of this progress tag can be managed by using JavaScript. Here is the line which we will be adding to manage the value attribute.
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>

Repeating the code at particular interval

At a particular interval we will post the existing width data and get back the new width from the PHP file. To execute the ajax code in a interval we will use one recursive timer in JavaScript. The code is here.
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.

Our server side PHP code.

There is nothing much in this code. We will only receive the existing width value and then increase by 20 and then return.
<?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.
progress bar gives the feedback of different stages of actual execution
Ajax Json XML
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    shaheb

    14-09-2013

    Thanks for create this type of web site.

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer