Solved

Display WAITING on webpage using PHP Ajax & mysql

Posted on 2012-04-11
5
455 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
ID: 37834750
that's how i'd do it - poll the server every N seconds just like you say.
0
 

Author Comment

by:sjtinsley83
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:sjtinsley83
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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
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)

895 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

11 Experts available now in Live!

Get 1:1 Help Now