Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is progress bar possible with php and javascript using ajax

I was wondering how to make progress bar like gmail.

I tried

<script src="jquery.js"></script>
<script>
$(function (){
    $.ajax({
        url: 'index.php',
        success: function(data) {
            $('#bar').html(data);
        }
    });
})
</script>
<div id="bar"></div>

And on index.php

[EDIT]: by sleep() i just meant to simulate continuous stream of output like multithreaded programs which is not supported in php.

<?php

for($i=0; $i<=10; $i++)
{
    sleep(1);
    echo "$i";
}

it seems that output is echoed out at once so i get result 012345678910 at once.

also i tried

setInterval(function (){
        $.ajax({
            url: 'index.php',
            success: function(data) {
                $('#bar').html(data);
            }
        });
    }, 1000);

Instead, i had trouble maintaining value of 'progress', so i did

<?php

session_start();

if(isset($_SESSION['value'])){
    if($_SESSION['value'] >= 10)
    {
        unset($_SESSION['value']);
    }
    else
    {
        $_SESSION['value']++;
    }
}
else
{
    $_SESSION['value'] = 0;
}

echo $_SESSION['value'];

as part of my php. But it seems that, i am calling ajax function on continuous interval.

My Question is,

  1. How does google use progress bar, while loginng in gmail. Do they get continuos 'stream' of data like i tried on my first example or send (regularly) request on some url (though not through ajax .. through JSONP or whatever) and upadate the page like second ?

  2. Can I do same with php , even if not with php, can I do it using javascript and other server side scripting language where multithreading is supported?

like image 985
hasExams Avatar asked Apr 22 '11 07:04

hasExams


People also ask

Can AJAX be used with PHP?

Start Using AJAX Today In our PHP tutorial, we will demonstrate how AJAX can update parts of a web page, without reloading the whole page. The server script will be written in PHP. If you want to learn more about AJAX, visit our AJAX tutorial.

Which is better AJAX or PHP?

PHP creates and outputs the Content to the Client Browser as it's a Server-Side Language and that's what it was built for, so on a request your code will access database, files etc. and then output the constructed html/text to the client. Ajax just gives the User a more Desktop like feel.

How to display progressing bar in PHP with Ajax?

Here we will use two files, one is out html file with Ajax and JavaScript code and the other file is server side PHP script. The server side php file will only receive one data and it will increase the value by 20 and return the same value back to the html file. To display a progressing bar we will use one small image of fixed height and width.

How to display a progress bar in WordPress?

To display a progressing bar we will use one small image of fixed height and width. The width we will change ( or increase ) based on the data we receive from the php file by Ajax. This is the code we will keep inside the div tag to display the progress bar. <div id="txtHint"><img src=bar.gif height=10 width=10></div>

What is the difference between basic progress bar script and demo?

There is not much difference in basic progress bar script and this. Through a timer it reads data from progress-bar2.php and manage the value attribute of progress tag. This page reads the data from the status table and returns the same to progress-bar2-demo.php through Ajax to display the progress bar image.

How do I display the progress of a PHP script?

Sometimes you may have a PHP script that takes a while to run. To keep the user informed on the progress of the script, and to reassure the user that something is actually happening, it would be advisable do display a progress bar. This can be achieved in a number of ways.


1 Answers

I'm not sure what exactly Gmail does for the progressbar, but you can achieve something similar in PHP, although it may be a bit tricky.

First, to explain why your examples don't work:

If you echo and sleep, like your first example, it will never work. Ajax performs a full request - that is, if the response does not finish, it will wait. When you loop and sleep, the request is not "closed" until the PHP script has finished executing.

If you use session, like in the other example, the problem becomes the session store. The store is typically locked during script execution, and it will not update itself to allow for the type of progress check you want.

What you could do is write progress to a file (or to a database) manually. For example:

file_put_contents('progress.txt', 1);

Then, have another script read the file and output the contents.

This should work, because file_put_contents opens, writes and closes the file.

Using some other language than PHP would make it easier. Being multithreaded would possibly make it easier, but is not a requirement. However, having a continuously running process would make it simpler (PHP only runs a process for the duration of your request and then exits)

like image 89
Jani Hartikainen Avatar answered Sep 28 '22 20:09

Jani Hartikainen