abdfahim Posted December 22, 2013 Share Posted December 22, 2013 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/284898-progress-bar-using-ajax-and-php/ Share on other sites More sharing options...
denno020 Posted December 29, 2013 Share Posted December 29, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/284898-progress-bar-using-ajax-and-php/#findComment-1463195 Share on other sites More sharing options...
mac_gyver Posted December 29, 2013 Share Posted December 29, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284898-progress-bar-using-ajax-and-php/#findComment-1463196 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.