Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display WAITING on webpage using PHP Ajax & mysql

Posted on 2012-04-11
5
Medium Priority
?
474 Views
Last Modified: 2012-06-20
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
0
Comment
Question by:Steve Tinsley
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37834750
that's how i'd do it - poll the server every N seconds just like you say.
0
 

Author Comment

by:Steve Tinsley
ID: 37839814
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
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37839864
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.
0
 

Author Comment

by:Steve Tinsley
ID: 37839978
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

0
 
LVL 23

Accepted Solution

by:
basicinstinct earned 2000 total points
ID: 37840339
rather than checking the db for each request that comes in couldn't you check the db once every second and then cache that value somewhere on the server where all requests could access it. that would take the pressure off your db at least (tho obviously the web server will still need to easily support 100 concurrent connections)

five minutes seems like a pretty long timeout when you are expecting fast responses... maybe better to let the user know sooner that there is a problem...

depending how accurate the start time needs to be i guess you will need to consider latency, geographic distance from the server, bandwidth (dial up users).
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question