Jump to content

Progress bar using Ajax and PHP


abdfahim

Recommended Posts

Hi,

I want to implement a progress bar. The idea is simple, I have file2.php where I set $_SESSION['progress'] value in each loop.

While file2 is working in background, I want to return the $_SESSION['progress'] value in main thread using below functions.

But the problem is, it is not working. While fprogress is being called in every second, the value is set only once at the end.

Can anybody please help?

Thanks,

var updateprog;
	
	function fprogress() {
	    var progxmlHttp = new XMLHttpRequest();
	    var progress;
	    var url = "file1.php";
	    progxmlHttp.onreadystatechange=function(){
		if (progxmlHttp.readyState==4 && progxmlHttp.status==200){
		    progress = progxmlHttp.responseText;
		    document.getElementById("progress").innerHTML ="Progress: "+progress+"% <br><br>"; 
		}
	    }
	    
	    progxmlHttp.open("GET", url, true);
	    progxmlHttp.send(null);
	}
	
	function updateAll(mid)
	{
	    var xmlhttp=new XMLHttpRequest();
	    updateprog = window.setInterval(fprogress,1000);
	    xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
		    window.clearInterval(updateprog);
		    document.getElementById("progress").innerHTML ="progress: 100% <br><br>"; 
		}
	    }
	    xmlhttp.open("POST","file2.php",true);
	    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	    params = "key=xxx";
	    xmlhttp.send(params);
	}

file1.php

<?php
    if (!isset($_SESSION)) {
	session_start();
    }
    echo $_SESSION['progress'];
?>

Link to comment
Share on other sites

Php won't return values whilst the script is still executing. Only at the end will any output actually be sent. So your echo will only be echo'ed at the end of the script processing, as per your findings.

 

The way you would do a progress bar (at least the way I think you do it, as I've not actually done it myself), is to also use cURL.

 

Denno

Link to comment
Share on other sites

using a session variable as the means of passing data between scripts won't work unless the script updating the value into the session variable executes a session_write_close() after every update and then executes a new session_start() to resume the session.

 

modifying a $_SESSION variable while a scripting is running, only changes the value of that variable in memory. it isn't actually stored in to the session data file (where a different script could get the value of it) until the first script ends or you use a session_write_close() statement.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.