May 22, 201115 yr Hello, Ive spent hours now trying to find a solution to this but no luck (n) anyway i need a php file to run every couple of seconds to update the amount of messages that are in there inbox i have my main php file which contains the main structure of the page/layout etc in the head of that document ive got <script type="text/javascript"> function loadcont() { $("#count").load("includes/msgcount.php"); var t=setTimeout("loadcont()",4000); } </script> and in the body i have a div with the id called count the javascript is initially being ran by <body onload="loadcont()"> and that parts working fine, the php file echos once into the file like its supposed to and then if i update the msgcount.php file and wait on the website for the page to refresh the info it doesnt, baring in mind the setTimeout is definatly working as i tested it with an alert() So in short words, is this possible? Thanks
May 22, 201115 yr Hello, Ive spent hours now trying to find a solution to this but no luck (n) anyway i need a php file to run every couple of seconds to update the amount of messages that are in there inbox i have my main php file which contains the main structure of the page/layout etc in the head of that document ive got <script type="text/javascript"> function loadcont() { $("#count").load("includes/msgcount.php"); var t=setTimeout("loadcont()",4000); } </script> and in the body i have a div with the id called count the javascript is initially being ran by <body onload="loadcont()"> and that parts working fine, the php file echos once into the file like its supposed to and then if i update the msgcount.php file and wait on the website for the page to refresh the info it doesnt, baring in mind the setTimeout is definatly working as i tested it with an alert() So in short words, is this possible? Thanks Yeah, that should work fine. If it's not updating then I guess it must be a problem with your msgcount.php file. Can you post the code for it?
May 22, 201115 yr Author Yeah, that should work fine. If it's not updating then I guess it must be a problem with your msgcount.php file. Can you post the code for it? <?php require_once("config.php"); //require_once("functions.php"); //logincheck(); $cid = $_SESSION['id']; $sqlquery = "SELECT * FROM `messages` WHERE (uid2 = '$cid' AND read1 = '0')"; $result = mysql_query($sqlquery)or die(mysql_error()); $unreadmsgs = mysql_num_rows($result); //to get the total rows echo $unreadmsgs; ?> thats the msgcount.php code, if i run the msgcount.php script on its own it works though and updates accordingly Edited May 22, 201115 yr by web-itec
May 22, 201115 yr Author Edit* the reason it didnt work is because the session must have ended for some reason and it wasnt getting the id to get the messages, so msgcount.php does work but the loading of it through javascript doesnt 2nd EDIT** it works now, just so you know what went wrong, it was because i didnt import the latest jq <script src="http://code.jquery.com/jquery-latest.js"></script> Edited May 22, 201115 yr by web-itec
May 22, 201115 yr Instead of recursively calling the function with a setTimeout delay, simply use this: <script type="text/javascript"> window.onload = function(){ setInterval(loadcont, 4000); }; </script> Or the equivalent jQuery... <script type="text/javascript"> $(document).ready(function(){ setInterval(loadcont, 4000); }); </script>
Create an account or sign in to comment