This example posts the current width to the same progress-bar.php processor used by the demo. The processor returns the next width as plain text, and polling stops when the value reaches 200.
async function updateProgress() {
const form = document.forms[0];
const body = new URLSearchParams({ width: form.width.value });
const response = await fetch('progress-bar.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body
});
if (!response.ok) return;
const width = Number(await response.text());
form.width.value = width;
document.getElementById('p1').value = width;
if (width < 200) {
setTimeout(updateProgress, 1000);
}
}<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, ajaxFunction() runs once per second while the width is less than 200.
<?php
header('Content-Type: text/plain; charset=UTF-8');
$width = filter_var($_POST['width'] ?? null, FILTER_VALIDATE_INT, [
'options' => ['min_range' => 0, 'max_range' => 200]
]);
if ($width === false) {
http_response_code(400);
echo 'Invalid width';
exit;
}
echo min(200, $width + 20);
?>
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 a complete browser-side version using the same Fetch contract shown above. It updates the HTML5 progress element directly and avoids rebuilding markup with innerHTML.
<form id="progressForm">
<input type="hidden" name="width" value="20">
<button type="submit">Start</button>
</form>
<progress value="0" max="200" id="p1"></progress>
<span id="progressText">0</span>
<script>
const form = document.getElementById('progressForm');
const progress = document.getElementById('p1');
const progressText = document.getElementById('progressText');
async function updateProgress() {
const body = new URLSearchParams({ width: form.elements.width.value });
const response = await fetch('progress-bar.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body
});
if (!response.ok) return;
const width = Number(await response.text());
form.elements.width.value = width;
progress.value = width;
progressText.textContent = String(width);
if (width < 200) setTimeout(updateProgress, 1000);
}
form.addEventListener('submit', (event) => {
event.preventDefault();
form.elements.width.value = 20;
progress.value = 0;
updateProgress();
});
</script>
Same way here is our progressbar.php file.
<?php
header('Content-Type: text/plain; charset=UTF-8');
$width = filter_var($_POST['width'] ?? null, FILTER_VALIDATE_INT, [
'options' => ['min_range' => 0, 'max_range' => 200]
]);
if ($width === false) {
http_response_code(400);
echo 'Invalid width';
exit;
}
echo min(200, $width + 20);
?>
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. | |