Welcome Guest, Not a member yet? Register   Sign In
[solved] Real time log with AJAX/JSON?
#1

[eluser]h311m4n[/eluser]
Hi guys,

This is a simple question, however I ain't no javascript expert so I'd appreciate any help.

Here's what I need: I have a simple controller function that processes data in a foreach loop. If the script is successful, it echoes a message saying so, if not, it echoes a message saying it failed. It does this for each item in the loop.

Obviously, with PHP, the issue is that the log file only shows up once the script has finished. I would like this to be in real-time. Each time a loop completes, it should echo the success or fail message.

Obviously, I need AJAX/JSON to do this, but I can't figure out how to do this. Here's what I have now:

Controller:
Code:
public function generatebill(){
     $data['users'] = $this->billingmodel->getUsers();
          foreach ($data['users'] as $user){
                $script_start = microtime(true);
                $userdata['user_bill'] = $this->billingmodel->getUserCalls($user->username);
                $html = $this->load->view('pdftemplate',$userdata,true);
                pdf_create($html,$user->username);
                $script_stop = microtime(true);
                $script_duration = $script_stop - $script_start;

                    if(file_exists('test/'.$user->username.'.pdf')){
                        echo '<span style="color:green">'.$user->username.' PDF Generated in </span>'.$script_duration.' seconds'.br();
                    }

                    else{
                        echo '<span style="color:red">'.$user->username.' PDF Generation error.</span>'.br();
                    }
            }
        }

The view:
Code:
&lt;?php
     echo form_label('Launch the billing loop','go');?&gt;
     &lt;input type="button" value="Now" name="go"&gt;
        <div id="result"></div>
        [removed]
            function processBills(){
                $.ajax({
                    url:('generatebill'),
                    type:'POST',
                    success:function(msg){
                        $('#result').append(msg);
                    }
                });
            }
        [removed]

This code works just fine, however it still only appends all the messages once the script completed.

Any ideas?

BTW: i tried to flush after the echo, but this doesn't change anything. It does create only 4 PDFs (I only got 4 users to test wirh right now) too. Output buffer is disabled in php.ini.
#2

[eluser]alexaaaaaaaaaa[/eluser]
the submit button i think must be type="submit"
in your function you must have something like if the button has been submited then create the pdf in your url of ajax use something like &lt;?php echo site_url('generatebill'); ?&gt; also use on error to print the error if there's any and finally use firebug just in case something it's wrong.
#3

[eluser]IgnitedCoder[/eluser]
You might want to look into APE - http://www.webresourcesdepot.com/open-so...ngine-ape/ - Ajax Push Engine. Might be overkill for what you need. Or try this script : http://ajaxian.com/archives/pquery-php-and-jquery.
#4

[eluser]skunkbad[/eluser]
[quote author="h311m4n" date="1305043020"]Obviously, I need AJAX/JSON to do this...[/quote]

You don't need ajax to echo things as they are looped, you simply need to figure out how to turn off output buffering or flush the output in PHP/CodeIgniter.

See this thread:
http://ellislab.com/forums/viewthread/110755/#814729
#5

[eluser]h311m4n[/eluser]
Thanks for the replies guys!

After messing around a bit, I simply used php flush and it would output the results on after the other. There was just a slight delay so I needed loop at least 20 times for it to start showing the results in "real time".

Thanks for the links though, will take a look.

Cheers,




Theme © iAndrew 2016 - Forum software by © MyBB