Link to home
Start Free TrialLog in
Avatar of Steve Tinsley
Steve TinsleyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Display WAITING on webpage using PHP Ajax & mysql

I have only started looking into this but it could be complex...

I want to be able to display a WAITING screen on a php page whilst the value of ready in a mysql database is 0. When I update the mysql READY field to 1 the waiting screen goes.

The goal eventually will be:
User click to voting.php
User sees waiting box
When I want the user to start voting the first question shows.
User can then complete all questions to the end.

Do you have any thought on this??

I thought perhaps have an ajax call every second until ready =1...

Any other ideas.

Thanks

Steve
Avatar of basicinstinct
basicinstinct
Flag of Australia image

that's how i'd do it - poll the server every N seconds just like you say.
Avatar of Steve Tinsley

ASKER

The system im developing may have 100 browsers all on the waiting page at the same time.
This would mean 100 browsers all polling the mysql db every second.

Is there a better way of doing this?

I have been looking into LONG POLLING but dont quite understand it yet. Would this be the way to go? How different is this to a standard ajax call??

Steve
yes, i think every 1 second is a lot (which is why I said every "N" seconds), but you know your data and maybe you need that kind of instant feedback.

i have looked into this from time to time in the past and always ended up sticking with polling... server push / comet / long polling whatever you want to call it always involved leaving the connection permanently open... so instead of ever N seconds you have this connected open permanently.

if you could increase the interval you will reduce the load

in the past we have looked at how the page design can aid in perceived performance - rather than a loading screen where the user has nothing to read or do can you present the user with some reading (voting instructions or something)...
or failing that give them a quote of the day, pacman game etc :)

my last solution could poll easily for 20 seconds (slow service call) so we put the region that would be updated at the bottom of a screen which would take the user a minute to work through - they wouldn't even notice the slow service call.
The system im designing needs to start the voting for possibly 100 browsers at the same time (or within a fraction of a second).

From what ive read, the system may work like this...
What do you think?
JS
    function waiting(){
        $.ajax({
            type: "GET",
            url: "waiting.php",
            async: true,
            cache: false,
            timeout:300000, /* Timeout in ms */

            success: function(data){
                                if (data == 1){
                                            load vote question
                                 }
                                 else
                                 {
 				            $.blockUI();  /*  display please wait*/
                                            waiting()', /* Try again */
                                 }
            },
        });
    };

Open in new window



PHP
connect to db
$wait = 0
while $wait = 0 {
           query database
           sleep(1);
}
echo $wait

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of basicinstinct
basicinstinct
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial