Display data from long running PHP script with jQuery

When a PHP script is executed for a long time and it is not clear what it is happening, such as a long loop, a prolonged processing, rainy weather, or other magical things. That is, the desire to see what is happening with the data and at what stage of implementation is a script program. One of the options to use function flush() which clears PHP output buffer and all its contents are sent to the user's browser.

header('Content-Type: text/plain; charset=utf-8');
//unlimited script run time
	//data output
	echo "\nloop: $i progress: ".round($i/10)."%";
	//clear and send PHP output buffer
	//pause for clarity

Another option is to use a script to write data to a separate file and load this file from time to time in the browser. You will need to write data file from the script:

	//writing data to the file log.txt, you need to set file write permissions
	file_put_contents('log.txt', "loop: $i<br> progress: ".round($i/10)."%");
	//pause for clarity

Further data file will load jQuery load function:

<meta charset="UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var reload = setInterval(
		//load data from file and display in element id="progress"
	}, 1000); //every second
	<div id="progress"></div>
This entry was posted in Programming and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.