Search code examples
phpoutput-buffering

PHP Echo information and wait to remove it


What I'm trying to do in my code is show a set of numbers, wait for a specified amount of time, and then delete what was previously shown, and then show an input. I just started coding recently.

if(isset($_POST['Start'])){
ob_clean();
echo $num2;
sleep(2);
ob_clean();
echo "<form method='post' action=''>
(etc...)

Solution

  • If data has been sent to browser, php can't impact it. My solution is when you show your input, you attached a javascript function. okay, i thinks this code will help you

    <?php 
    function flush_buffers(){ 
        //This function make buffer send with out some problem
        ob_end_flush(); 
        ob_flush(); 
        flush(); 
        ob_start(); 
    } 
    ob_start();
    $title = "<span class='mytitle'>My Title</span>";
    echo $title; //You send your information
    flush_buffers();
    
    $input = "<input type='text'>"
    $js_code = "<script type='text/javascript'>$('.mytitle').remove()</script>";//with jQuery :)
    echo $input.$js_code;
    flush_buffers();
     ?>