Solved

Display WAITING on webpage using PHP Ajax & mysql

Posted on 2012-04-11
5
453 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:sjtinsley83
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

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

Author Comment

by:sjtinsley83
Comment Utility
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
Comment Utility
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:sjtinsley83
Comment Utility
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 500 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now