imperium2335 imperium2335 - 6 months ago 23
Ajax Question

AJAX live feed from PHP

Is there a way to have an ajax script make updates to a html element while the PHP script is being parsed and not only when it has completed? I.e.:

PHP:

<?PHP
echo 'stage 1';
sleep(3);
echo 'stage 2';
sleep(3);
echo 'Finished';
?>


What I would hope to see happen to the html element is that first it says 'stage 1', then 3 seconds later, 'stage 2' etc.

Would flushing the OB achieve what I want or is there a better way of doing this?

Answer

Sorry but to you can do this you need create this logic on the front-end with javascript/jQuery and use only back-end(php) to get the feeds.

setInterval(function(){
    //jQuery code..
    $.ajax({
        url: 'url_to_get_feeds',
        data: {}, //if need put some param on request
        success: function(callback){
            //logic to put call content to html
            //callback recive all content gerenated on the page 'url_to_get_feeds'
            $('body').append(callback)
        }
    })
});
Comments