views:

219

answers:

4

Hi Everyone....

I have a script that runs for quite a while and i am using ob_start() to buffer the output and print the result after the script finishes executing...

I need to show some loading animation while the script executes and replace it with the output when the script finishes executing...

I know that it can be done quite beautifully with javascript and jquery.... I was just wondering whether it can be done using php alone...?

Is there any library that allows php to do this...?

Thanks for your replies....

+2  A: 

Unfortunately you can't use PHP (Which is running on the server) to alter the HTML that has already been sent to the client.
In other words you are probably going to have to use JavaScript to achieve what you want to do.

Yacoby
I guess i can call some javascript function just before printing the output to the screen....?
SpikETidE
No, JavaScript is run on the client, so you'll have to print the loading page, then use some JavaScript to make an AJAX request and alter the HTML with JS/redirect to the page with the data.
mdm
@SpikeETidE You could print some Javascript that hid the loading animation just before you printed the results.
Yacoby
A: 

You can't replace content you have already sent with just PHP. You can only append to it. I don't see any practical way of doing what you want to do with PHP alone.

Manos Dilaverakis
I am not trying to modify the content already sent.. i am just trying to remove the loading animation that will be in a separate div just before printing the output....
SpikETidE
@SpikETidE The loading animation is content already sent. Removing it counts as modifying content.
Manos Dilaverakis
A: 

You could try something like this:

<!Doctype>
<title>hide later</title>
<p>Wait 3 seconds</p>
<?php
ob_start();
sleep(3);
?>
Done!
<style>p{display:none}</style>
<?php
ob_end_flush();

But … it works in Opera only. :) IE, WebKit and Gecko will wait for the last bit before they render anything. Oh, and it’s invalid markup.

toscho
A: 

I tried the following....

<script type="text/javascript">
  function rep(){
      var elem = document.getElementById('temp');
      elem.innerHTML = '';
      elem.innerHTML = "<a href=\"index.php\" style=\"color:#6FF; font-size:15px;\"> Go Back To File Browser </a>";
  }
</script>

<div id="temp">
  <img src="Loading.gif" alt="This Process May Take Few Minutes.. Please Wait...." />
</div>

<?php
    ob_start();
    for($i=0; $i<999999; $i++){
        echo $i;
        echo '<br/>';
    }
    $output = ob_end_flush();
    echo "<script type=\"text/javascript\">";
    echo "rep();";
    echo "</script>";
    echo $output;
    ?>

But it still shows the output before the script gets finishes executing.... I intended the result to be printed as a whole after the script finishes.. Not bit by bit as it does now...!!

SpikETidE