hi friends

i have develpoed a prog. for pay calculation for my office in php.
now i want to show some thing like employee name in my pge which is currently processing or progress bar with persent if calcution has done.
plz note i m useing POST methord

Dani AI

Generated

Short summary and practical pattern that complements , and : streaming output with PHP's buffers (what demonstrated) can work for simple pages but is fragile behind proxy buffers or PHP-FPM. Submitting the employee via AJAX (as suggested) is the right idea; for long calculations the most robust pattern is: accept the POST to start work, return a job id immediately, run the heavy job asynchronously, and have the client poll (or use SSE/WebSockets) for a lightweight progress endpoint.

Example client flow (start job + poll):

fetch('/start.php', {
  method: 'POST',
  body: new URLSearchParams({ employee: 'Alice' })
})
.then(r => r.json())
.then(({ job }) => {
  const poll = () => fetch('/progress.php?job=' + job)
    .then(r => r.json())
    .then(s => {
      updateProgressBar(s.percent);
      if (s.percent < 100) setTimeout(poll, 500);
    });
  poll();
});

Minimal server-side approach (concept): a background worker writes progress to a small JSON file or a cache/Redis key; progress.php reads and returns that JSON. If the long script needs session data, call session_write_close() early so progress polls are not blocked by PHP session locking. When launching a background job from a web request, escape arguments and run detached (use escapeshellarg() and background execution), or better, push the job to a queue system.

Troubleshooting and cautions: output-buffer flushing often fails behind nginx/fastcgi/proxy buffering, so do not rely on browser-visible flush for reliable progress. Ensure file permissions and atomic writes for progress storage, clean up temp files after completion, and validate/escape any user-supplied values used in job IDs or shell commands. For production use, prefer a real queue (Redis/Beanstalkd) or SSE/WebSockets for low-latency updates. Credit to @SPeed_FANat1c for the working snippet that solved the OP's case; the above gives a more portable, scalable pattern.

Recommended Answers

All 3 Replies

I have saved code snipped with flush:

PHP updating page when function is not finished
Tested, works. But does not work when fiddler is running.

public function index() {

        if (ob_get_level() == 0) {
            ob_start();
        }
        echo str_pad('Loading... ',4096)."<br />\n";
        $d = 0;
        for ($i = 0; $i < 25; $i++) {
            $d = $d + 11;
            $m=$d+10;
            //This div will show loading percents
            echo '<div class="percents">' . $i*4 . '%&nbsp;complete</div>';
            //This div will show progress bar
            echo '<div class="blocks" style="left: '.$d.'px">&nbsp;</div>';
            flush();
            ob_flush();
            sleep(1);
        }
        ob_end_flush();
    }

Clear screen when using this way of page updating:
private function clr_scr() {
        echo "<script type='text/javascript'>\n";
        echo "document.body.innerHTML = ''";
        echo "</script>";
    }

This is not ajax, but you can use it.

Not sure how this would work with ajax, havent' tried.

I'm not sure what you really want but try this.

In your JavaScript code, submit the name of your employee via form and perform an ajax call so as to not refresh the page.

Requirements:
jquery framework (widely used js framework that rocks!) - http://jquery.com/download/
jquery ujs so as to handle all ajax calls across your site - https://github.com/rails/jquery-ujs

HTML CODE:

<form action="youraction" method="post" id="form-id" "data-remote" => "true">
<input type="text" name="employee_name"/>
<div id="loader"></div>    
<button type="submit">Submit</button>
</form>

<div id="employee-data"></div>

JS CODE:

$(document).ready(function() {

  // ajax:before is executed upon submitting your form
  // you can insert a loader here or do some validation before sending your ajax call  
  $('#form-id').on('ajax:before', function() {
    // put your code here
    $('#loader').show();
  });

  // ajax:success runs when the server returns a successful call
  // display the data returned by your server here
  $('#form-id').on('ajax:success', function(event, data) {
    // put your code here
    // data depends on what kind of data your server responds (json or html format)
    $('#employee-data').html(data);

    // hide your loader
    $('#loader').hide();
  });

});

sorry to responce so late, my problem was solved through the code given by "SPeed_FANat1c"
but I would like to thanks to both of u along daniweb.com for the greate help . thank u to all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.