Is there any way to count users in my chatroom and display whos chatting using javascript or ajax? code in php below has issues, but this is what it should do.

<?php


$chatnames = file('http://client11.addonchat.com/scwho.pl?id=292747&plain=1');
$indx = count($chatnames);
$indxcom = $indx - 1;
if ($indx == 0) {
echo "<b>No one is in the chat room at the moment</b>";
}
else{
echo "<b>People Chatting:</b>&nbsp;&nbsp;";
 for($i = 0; $i < $indx; $i++) {
 $name = explode('  ', $chatnames[$i], 2);
echo " $name[0]";
 if ($i < $indxcom) {
    echo ", ";
}  

  }
}

?>

I was not able to use php on my website software directly so I was hoping to to remotely host this code and display the data by using an iframe sourced from the remote file, however various issues arose.
could I do the same thing with ajax or javascript?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I found A few Errors But Still no output, Help?

<?

Header("content-type: application/x-javascript");
$chatnames = file('http://client11.addonchat.com/scwho.pl?id=292747&plain=1');


$indx = count($chatnames);
$indxcom = $indx - 1;

if ($indx == 0){
echo "document.write(\"No one is in the chat room at the 

moment"\)"; 
}

else{
echo "document.write(\"<b>Members Chatting:</b>"\)";


  for($i = 0; $i < $indx; $i++) {

    $name = explode('      ', $chatnames[$i], 2);

    echo "document.write(\"$name[0]"\)";

    if ($i < $indxcom) {

   echo "document.write(\","\)"; 
}  

  }
}
?>

Edited 3 Years Ago by mike_2000_17: Fixed formatting

using ajax to call a php fuction:
put this where you want resutls:

<div id="results" style="display:inline; width:100px; height:20px;"></div>

include this ajax library in the page you want the results to display on.
http://www.codeproject.com/KB/ajax/SAL/sal_src.zip

<script type="javascript" src="path_to_ajax_lib"></script>

create php page with your function in it and do this:

<?php 
function online(){
      //i write this asumming that the script you are getting contents from is correct
      //and is returning correct input in the form of an array
     $online =  file('http://client11.addonchat.com/scwho.pl?id=292747&plain=1');
     return $online;
}
if($_GET['results']=='membernames'){
            $users = online();
            foreach($users as $u){
                   echo $u;
           }
?>

insert this into your page after the div:

SetInnerHTMLFromAjaxResponse('your_php_page.php?results=membernames', 'results');

Thanks For you fast help , unfortunately Ive discoved the file no longer exist!
Ive saved this information though, Im sure it'll come in handy!

Thanks again!

using ajax to call a php fuction:
put this where you want resutls:

<div id="results" style="display:inline; width:100px; height:20px;"></div>

include this ajax library in the page you want the results to display on.
http://www.codeproject.com/KB/ajax/SAL/sal_src.zip

<script type="javascript" src="path_to_ajax_lib"></script>

create php page with your function in it and do this:

<?php 
function online(){
      //i write this asumming that the script you are getting contents from is correct
      //and is returning correct input in the form of an array
     $online =  file('http://client11.addonchat.com/scwho.pl?id=292747&plain=1');
     return $online;
}
if($_GET['results']=='membernames'){
            $users = online();
            foreach($users as $u){
                   echo $u;
           }
?>

insert this into your page after the div:

SetInnerHTMLFromAjaxResponse('your_php_page.php?results=membernames', 'results');

The above expects HTML output from a page, not JavaScript output as given in the PHP function.

The original post also mentions having the PHP file on a different domain. AJAX via the XMLHttpRequest object requires that the files be on the same domain. (At least untill cross domain XMLHttpRequest becomes implemented in all browsers as per the W3C specifications)

A workable solution would have be to use "JavaScript Remoting". You can include a JavaScript file after the page has loaded.

Simple example being:

document.write('<script type="text/javascript" src="http://example.com/your/remote/file.php"></script>');

The src attribute would be your remote PHP file. It would have to return a JavaScript function call, as it currently does in your PHP script.

PS: you can also use "Iframe Remoting" which is similar but uses Iframes instead. That is limited to JavaScript function calls also because of the "same origin policy" in effect in XMLHttpRequest also.

This article has been dead for over six months. Start a new discussion instead.