web_master Posted May 21, 2013 Share Posted May 21, 2013 Hi, I want to see who is online in chat, but as I see dont work scripts (mixed Ajax and PHP). I got 3 files. index.php (in header) <script type="text/javascript"> $(document).ready(function () { function reload() { $("#content").load("update.php"); } setTimeOut(reload, seconds*1000) } </script> <script type="text/javascript"> setInterval("getList()", 10000) // Get users-online every 10 seconds function getList() { $.post("getList.php", function(list) { $("#listBox").html(list); }); } </script> (in body) <div id="listBox"></div> update.php <?php session_start(); mysql_query(' UPDATE `users` SET `lastActiveTime` = NOW() WHERE `reg_nick` = "' . $_SESSION['reg_nick'] . '" ') or die(mysql_error()); ?> getList.php <?php session_start(); if (!$_SESSION["reg_nick"]) { die; // Don't give the list to anybody not logged in } else { $users = mysql_query('SELECT * FROM `users` WHERE `lastActiveTime` > NOW()-60 ') or die(mysql_error()); $output = "<ul>"; while($row=mysql_fetch_array($users)) { $output .= "<li>".$row["reg_nick"]."</li>"; } $output .= "</ul>"; print $output; } ?> Thanks in advanced for help Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/ Share on other sites More sharing options...
DaveyK Posted May 21, 2013 Share Posted May 21, 2013 You say its not working. What if you do a console.log() and then check it in firebug. Also, Im pretty the php needs to return a JSON object in order for the JS to read it Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431311 Share on other sites More sharing options...
web_master Posted May 21, 2013 Author Share Posted May 21, 2013 I just try to learn Ajax, and dont know much about Ajax - so I dont understand what can I do... how to return from php... Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431326 Share on other sites More sharing options...
DaveyK Posted May 21, 2013 Share Posted May 21, 2013 Ajax is nothing but Javascript and PHP combined. What you want to do is something like function test () { console.log('test'); } setInterval('test', 1000); Then, if you check your console in, say, firebug (if you use firefox) or in the chrome console you should see that its being logged every second. and see if that works Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431327 Share on other sites More sharing options...
codefossa Posted May 21, 2013 Share Posted May 21, 2013 Why are you using $.post() but not actually sending data? The second parameter should be the post data. You may have wanted $.get(), but since you're just putting it into a container, you can use $.load(). Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431390 Share on other sites More sharing options...
web_master Posted May 22, 2013 Author Share Posted May 22, 2013 The first script need to update the update.php file every X second - update from session ( $_SESSION['reg_nick'] ) and this is a first problem, that don't update, and I don't know where is a problem javascript: <script type="text/javascript">$(document).ready(function () { function reload() { $("#content").load("update.php"); } setTimeOut(reload, seconds*1000)}</script> php <?phpsession_start(); mysql_query(' UPDATE `users` SET `lastActiveTime` = NOW() WHERE `reg_nick` = "' . $_SESSION['reg_nick'] . '" ') or die(mysql_error());?> The second script need to reload from getList.php the users who is online: javascript: <script type="text/javascript"> setInterval("getList()", 10000) // Get users-online every 10 secondsfunction getList() { $.post("getList.php", function(list) { $("#listBox").html(list); });}</script> php; <?phpsession_start();if (!$_SESSION["reg_nick"]) { die; // Don't give the list to anybody not logged in} else {$users = mysql_query('SELECT * FROM `users` WHERE `lastActiveTime` > NOW()-60 ') or die(mysql_error());$output = "<ul>";while($row=mysql_fetch_array($users)){$output .= "<li>".$row["reg_nick"]."</li>";}$output .= "</ul>";print $output;}?> So, at the end in index.php file, where is the <div id="listBox"></div> I want to see the list of online users. I didn't say that will be a simple chat, and I didn't do something like this before... Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431516 Share on other sites More sharing options...
DaveyK Posted May 22, 2013 Share Posted May 22, 2013 Please put your code in code blocks, identified by the "<>" in the top menu when writing a post. Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431537 Share on other sites More sharing options...
web_master Posted May 22, 2013 Author Share Posted May 22, 2013 The first script need to update the update.php file every X second - update from session ( $_SESSION['reg_nick'] ) and this is a first problem, that don't update, and I don't know where is a problem javascript: <script type="text/javascript"> $(document).ready(function () { function reload() { $("#content").load("update.php"); } setTimeOut(reload, seconds*1000) } </script> php <?php session_start(); mysql_query(' UPDATE `users` SET `lastActiveTime` = NOW() WHERE `reg_nick` = "' . $_SESSION['reg_nick'] . '" ') or die(mysql_error()); ?> The second script need to reload from getList.php the users who is online: javascript: <script type="text/javascript"> setInterval("getList()", 10000) // Get users-online every 10 seconds function getList() { $.post("getList.php", function(list) { $("#listBox").html(list); }); } </script> php; <?php session_start(); if (!$_SESSION["reg_nick"]) { die; // Don't give the list to anybody not logged in } else { $users = mysql_query('SELECT * FROM `users` WHERE `lastActiveTime` > NOW()-60 ') or die(mysql_error()); $output = "<ul>"; while($row=mysql_fetch_array($users)) { $output .= "<li>".$row["reg_nick"]."</li>"; } $output .= "</ul>"; print $output; } ?> So, at the end in index.php file, where is the <div id="listBox"></div> I want to see the list of online users. I didn't say that will be a simple chat, and I didn't do something like this before... I'm sorry, I'm omitted to put scripts in CODE... Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431556 Share on other sites More sharing options...
DaveyK Posted May 22, 2013 Share Posted May 22, 2013 First things first. You are trying to tackle two issues at the same time, which is great if you are up for it. Focus on one of the problems and leave the other one. I would say you focus on the first script first. The update script. First of all, I recommend you use firefox. If you do, install firebug add on. This allows you do inspect element, change the html live and above all: monitor ajax calls (and a lot more). If you use chrome, this is available without add on but I prefer firebug anyway. Comment out the js for the second script and focus on the first. What you are going to want to do is right click and then inspect element with firebug, then click console on the top left. You should see a list of the ajax calls. if the timeout is working you should see one every second. If its not working, your console will be empty and the issue lies with the js. Keep me posted. Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431578 Share on other sites More sharing options...
web_master Posted May 22, 2013 Author Share Posted May 22, 2013 With firebug I find that the Getlist is working, but the update is not working, in Dreamweaver is a syntax error, but I don't know what is the problem.... Somewhere here.... <script type="text/javascript"> $(document).ready(function () { function reload() { $("#content").load("update.php"); } setTimeOut(reload, seconds*1000) } </script> Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431596 Share on other sites More sharing options...
codefossa Posted May 22, 2013 Share Posted May 22, 2013 setTimeout() rather than setTimeOut() should work. Although, you also need to define your seconds variable. $(document).ready(function() { var seconds = 5; setTimeout(function() { $("#content").load("update.php"); }, seconds * 1000); }); Quote Link to comment https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/#findComment-1431636 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.