Q:

Use AJAX to check for a new message

Quite simply, I need to alert the end user when they have a new private message.
From a combination of research and other opinion, I realise I need to use AJAX for this.
The mysql query would be
SELECT id FROM tbl_messages WHERE to_viewed = 1

So when someone sends a message, I want an alert to popup on the screen to inform the user without a page reload.
I have absolutely no idea what I am doing, but know what I want.
Really need help with this, AJAX is definitely something I want to improve as it opens up greater possibilities!
Thanks

A:

Using jQuery for brevity, if you don't have any JavaScript experience I recommend learning. 
var check;
function checkForMessages() {
    $.get("/newMessages.php", function(data) {
        if(data == 1) {
            //There are new messages
            clearInterval(check);
            alert("You have new mail!");
        }
    });
}

check = setInterval(checkForMessages, 60000);

The above JavaScript will ping the server every 60 seconds. The script "newMessages.php" should return '1' if there are new messages. You have the query already written so I think you can figure it out.        

