-4

please, I need an ajax script that will add to the count of unread messages without a full page refresh and subtract onclick of a message. Something very similar to inbox messages count in most email clents. I will truly appreciate any useful help. Thank you

4
  • 4
  • I have the php and mysql scripts, I only need some help with the ajax. I'll appreciate it if you can help. Thank you. Apr 13, 2012 at 16:43
  • Read that link. Show what you tried and explain where you're stuck/what isn't working.
    – Mat
    Apr 13, 2012 at 16:43
  • @Mat: Nice one, I'll be using that from now on! Apr 13, 2012 at 16:46

2 Answers 2

0

Use javascript's setInterval function to check whether an unread message is available. Example:

setInterval(function() { // ajax request to check for unread messages }, 1000);

The ajax check in the above example will execute in every 1 second (1000 milliseconds). You can change the interval as you like, say 5000 (5 seconds).

1
  • I'm aware of setInterval(), what I need help with really is the ajax request to make. I'll appreciate an explicit help. Thank you. Apr 13, 2012 at 16:53
0

Make a function that can call different php files.

function useAjax(url1, area, send1)

{   

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  } else {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function() {

  if (xmlhttp.readyState==4 && xmlhttp.status==200) {

    document.getElementById(area).innerHTML=xmlhttp.responseText;

    }

  }

xmlhttp.open(POST,url,true);


     xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');


xmlhttp.send(send1);//to send post values "id="+id+"&message="+message"



return false;

}

So you could use this function to call "get_messages.php" or "read_message.php" and set the Post vaules accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.