Demon Demon - 7 months ago 30
PHP Question

javascript notification checker script Uncaught Reference Error

I have this notification checker and i don't know whats wrong with this js code, i have errors on checkMessage() : Uncaught Reference Error: checkMessage is not defined

the js code:

function checkMessage(){

if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function() //when the request is submitted...
{
if (xmlhttp.readyState==4 && xmlhttp.status==200 //...and everything is ok...
{
if(xmlhttp.responseText>0){ // if the response text is greater than 0

document.title="("+xmlhttp.responseText+")"+"My Social Network - Welcome";

document.getElementById('checkMsg').innerHTML="<b>" + xmlhttp.responseText+" Notifications</b>";
}
else
document.title="My Social Network - Welcome";

document.getElementById('checkMsg').innerHTML="No notifications";
}
}

xmlhttp.open("GET","checkMessage.php?user_id="+"1",true);
xmlhttp.send();
}


the checkmessage.php code:

<?php
include 'dbm.php'; //database settings from an external file

$user = $_SESSION['user'];

$get_count_messages = mysql_query("SELECT message FROM notifications ");
$row = mysql_num_rows($get_count_messages); // count how many rows the query returned

echo "$row"; //return the value
?>


The html code:

<html>
<head>

<script type="text/javascript" src="notifications.js"></script>
<script type="text/javascript">
setInterval("checkMessage()", 1000);
</script>
</head>
<body>
<div id="checkMsg"></div>
</body>
</html>

Answer

I think your setInterval function should look like:

setInterval(checkMessage, 1000);

Because setInterval expect a function as parameter.