0

little issue over here.

I have a php function that is called via AJAX and looks like this:

function processActiveDirectory(){
    $var = new GetLDAPUsers;

    echo "Getting Users from Active Directory.... <br />";        

    $adusers = $var->getAllUsers();
    echo "setting up images.... <br />";
    // processing more stuff
    echo "finished";
}

I'm trying to get a "live- log" echo. Meaning before every step the echo should output to a Log area, one step after another. So the user knows what's going on.

But the Problem is, that the log doesn't appear during the process, it just fills in at the whole text at the end of the process. Everything else works fine. The Log just doesn't appear at runtime, but after the function is finished it appears at the right position.

My AJAX call:

jQuery(document).ready(function($) {
    $('#lii-form').submit(function() {
        data = {
            action: 'lii_map_images'
        };

        $.post(ajaxurl, data, function(response){
            $('#lii_log').html(response);
        });
        return false;
    });
});

This is how it's build:

Ajax and PHP File

Edit

Other than in this thread I'm already using an ajax call, to call the function. It's within the called function that I'm echoing stuff...

Edit 2

I'm using wordpress

Sorry I can't offer more informations, because of enterprise restrictments.

Community
  • 1
  • 1
Zanidd
  • 133
  • 13

2 Answers2

4

This is a short over-view on your need. Please develop further with this idea.

This uses two AJAX calling - one for the main process and other for progress:

Script:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
    //Start the main process
    $.ajax({
        url: 'main.php',
        success: function(data) {
        }
    });

    function getProgress(){
        $.ajax({
            url: 'progress.php',
            success: function(data) {
                $("#progress").html(data);
                if(data != "finished"){
                    getProgress();
                }
            }
        });
    }

    //Start the progress section
    getProgress();
</script>
<div id="progress"></div>

main.php

<?php
$arr = ['Getting Users from Active Directory....','setting up images....','finished'];
foreach($arr as $value) {
    session_start();
    $_SESSION["progress"]=$value;
    session_write_close();
    sleep(1);
}

progress.php

<?php
session_start();
sleep(1);
echo $_SESSION["progress"];

So your processActiveDirectory will come under Main.php and echo should be replaced with SESSION variable

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
  • like this? $adusers = $var->getAllUsers(); $_SESSION["progress"]= "setting up images....
    ";
    – Zanidd May 18 '16 at 11:42
  • 1
    @Zanidd Yes, Remember to start and write close the session for every progress update, only then it is detected. (Am trying to find an alternative for multiple start and close) – Thamilhan May 18 '16 at 11:44
  • It didn't really translate that well into my code, I think it's mainly because it's a wordpress thing – Zanidd May 19 '16 at 06:12
  • hi, in recent days, is there any new and advance technique instead? – frozenade Jun 13 '22 at 10:26
0

I think there is no need in such thing as LOG process WITH AJAX. AJAX is too heavy thing and it could be a bad design if you want it. It's better to use web sockets or not use at all

user2434435
  • 127
  • 5