Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/278231-setinterval-and-update-dont-work/
Share on other sites

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

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... :(

 

 

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...

 

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.

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>

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);
});
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.