开发者

New message popup

I currently have a messaging system. I am looking to make it so that when a user receieves a new message, they get an alert. A popup perhaps?开发者_StackOverflow Like a message box saying "you have a new message".

How could I achieve this? Thanks


You can't really do this in php, as normal using of php implies no direct user interaction. What you need to do is to implement the relevant user interaction code in for example javascript, and do polling towards the server hosting the php (using AJAX or similar), to see if an popup should be shown or not.


This is not only a php problem. You need to make a database table like this:

messageID
contents
...
popuped
userID

Then make a page who gets the count of messages who are not popuped already:

SELECT COUNT(*) AS nr FROM messages WHERE userID = :userID AND popuped = 0

And after that set popuped to 1:

UPDATE messages SET popuped = 1 WHERE userID = :userID

And print the nr field.

Then for every page on your site you need to make a scheduled ajax request (for example every 20 seconds) who loadeds that page. If the result is higher as zero you need to show that the user has messages.

For the ajax request take a look for example at the mootools javascript framework (or jQuery, Prototype, etc.)


Are you looking for a 'live' message, as in an alert displays while the user is doing other things? Or are you looking for a 'static' message, as in an alert that displays when the user opens a page? The static method can be achieved with server-side php. The live method will need to employ client side techniques. I recommend using jQuery and jGrowl and something like jQuery Timers for the live methods. Try this SO question about javascript timers here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜