使用ajax动态检查邮件收件箱?

I'm almost done coding a website that i had started a few months ago. I have the foundation done but, i wanna add some new features to it to make it quicker and add some bling to it.

Currently, I have system whereby php queries the db to see if there are any unread messages every time the page a new page request is made. if a user has an unread message, php echoes the number of unread messages inside of a quotations.

How could I use ajax or jquery to echo out the number of undread messages without having to make a new page request?

thanks

Create a script that only echoes the number of unread messages (without any other HTML) and then use jQuery to get it:

$('#span_with_numer_of_messages').load('your_unread_messages_script.php');

You could set a timed request, like this:

var element = $('...');

// new get request every minute - 60*1000ms
var interval = setInterval( function(){
    element.load('/phpfile.php');
}, 60000 );

In your /phpfile.php Output results and encode array to json format. For example you may have query

<?php
    include 'config.php';
    include 'opendb.php';        
    $query  = "SELECT message FROM messages";
    $result = mysql_query($query);
    $jarray = array();
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        $jarray[] = array("message"=>$row['message']);           
    }
    echo json_encode($jarray);        
    include 'closedb.php';
?>

Get json array with ajax.

  var messagesContainer = $('messages'); 
  $.getJSON('phpfile.php',function(data)
    {
      $.each(data, function(i,stuff){
          $("<div class='msg'>"+stuff.message+"</div>").prependTo(messagesContainer);
      }

    })