views:

96

answers:

5

Ok, so you know when you're answering a question and are in the middle of typing it, and someone else posts an answer to your question and you get a little popup that says there is a new answer to the question? My question is how do you do that? I think I have the basic concept down... A question is answered, added to the database. The page your on keeps checking the database for new answers, and if there is something new displays a popup. (I'm not sure if that's how it's done, but just and idea) Anyway, I'm trying to create an application with similar functionality to that popup using php, and jQuery / Ajax / something else? I have a page that will be on the screen and will display information from the database. What I need: to figure out how to get that popup to display only when there is new content added to the database. Thanks for the help!

I should also add... if anyone has any tutorials, or code snippets to share about the ajax / jquery integration with sql that'd be great. I'm pretty decent at PHP but totally new to ajax and jquery :-/

A: 

Create a timer in Javascript and call an AJAX endpoint that provides you with messages to diplay or that delivers HTML to place in a placeholder at the top of each page. If you get data returned then insert that into the page DOM using jQuery.

Lazarus
A: 

You got the basic concept - I don't know for certain, but this does seem to be the obvious way to do this.

As for the specific question - the result from the ajax call should indicate if an answer has been added or not. Only display the popup when the results indicates that a result has been added.

Oded
+5  A: 

when you first load the question page, also read the number of answers.

Now poll the server with ajax requests every few seconds/minutes that return the number of answers..

If the number of answers is greater than the number when you first loaded the question then show the message that additional answers have been posted..

Gaby
Any suggestions on how to poll the server with ajax?
Phil
While I haven't checked with the SO source, this method seems to be the most logical path.
Moses
@phil, use `setTimeout` or `setInterval` to control the frequency of the polling, and use the jQuery ajax capabilities.. Your ajax call would need a page on the server that receives the id of the question, checks the DB for answers to that question and return their count..
Gaby
@Gaby, just to make sure I understand. I make a PHP page that checks the DB and returns the last ID. Then the jQuery calls this PHP page and so the jQuery will now have the last ID, and if that ID is > the ID on the page I will call a pop up function... right?
Phil
@phil , the ID is the id of the question. What you need is also the number of answers for that question, and that is what you check with the initial number of answers .. It depends on how your DB is set up.. (check out the `count` function of your sql system)
Gaby
Ok awesome.. thanks. I'm using http://www.w3schools.com/php/php_ajax_database.asp sort of as a guide to do the ajax request. I think I've figured it out
Phil
Phil, if you're a really hardcore PHP guy, you might also check out http://www.xajax-project.org/ Some of my developers find it easier to use than other Ajax methods.
bpeterson76
@bpeterson that looks pretty interesting. I'll have to check that out
Phil
A: 

You can use jQuery to do almost all of this.

Create a script that will return the needed JSON data, probably just the id of the current article and then the count of answers. In your page, create a timer that performs an ajax call to the script every ~30 seconds. In your success callback function, compare the number of answers that were returned against the number of answers currently on the page. If the answer is greater, then perform a notification using the show() or fadeIn() functions. (You can either have the message HTML loaded when the page loads or you can append/prepend all of the html for the message with the ajax call.

This is a vague answer, but the question is actually very broad so you could do this in a million different ways. If you need some help with it, you can PM me.

Scott Harwell
+2  A: 

This is a tutorial on how to do the actual notifications.

As for the querying, it's a fairly simple AJAX call to the database on a timed interval to check for new results. If count > 0, then fire the notification process.

The refresh method on this tutorial could easily be re purposed for such notifications.

bpeterson76