User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 391,971 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,174 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 927 | Replies: 4
Reply
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

convert php function to ajax?

  #1  
Mar 13th, 2008
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?
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: convert php function to ajax?

  #2  
Mar 13th, 2008
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(\","\)"; 
}  

  }
}
?>

Last edited by Inny : Mar 13th, 2008 at 2:32 pm.
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote  
Join Date: Mar 2007
Location: georgia
Posts: 55
Reputation: hunkychop is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
hunkychop's Avatar
hunkychop hunkychop is offline Offline
Junior Poster in Training

Solution Re: convert php function to ajax?

  #3  
Mar 13th, 2008
using ajax to call a php fuction:
put this where you want resutls:
  1. <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
  1. <script type="javascript" src="path_to_ajax_lib"></script>
create php page with your function in it and do this:
  1. <?php
  2. function online(){
  3. //i write this asumming that the script you are getting contents from is correct
  4. //and is returning correct input in the form of an array
  5. $online = file('http://client11.addonchat.com/scwho.pl?id=292747&plain=1');
  6. return $online;
  7. }
  8. if($_GET['results']=='membernames'){
  9. $users = online();
  10. foreach($users as $u){
  11. echo $u;
  12. }
  13. ?>
insert this into your page after the div:
  1.  
  2. SetInnerHTMLFromAjaxResponse('your_php_page.php?results=membernames', 'results');
Last edited by hunkychop : Mar 13th, 2008 at 4:58 pm. Reason: fix code tags
toast
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: convert php function to ajax?

  #4  
Mar 13th, 2008
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!
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote  
Join Date: Sep 2005
Posts: 641
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: convert php function to ajax?

  #5  
Mar 15th, 2008
Originally Posted by hunkychop View Post
using ajax to call a php fuction:
put this where you want resutls:
  1. <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
  1. <script type="javascript" src="path_to_ajax_lib"></script>
create php page with your function in it and do this:
  1. <?php
  2. function online(){
  3. //i write this asumming that the script you are getting contents from is correct
  4. //and is returning correct input in the form of an array
  5. $online = file('http://client11.addonchat.com/scwho.pl?id=292747&plain=1');
  6. return $online;
  7. }
  8. if($_GET['results']=='membernames'){
  9. $users = online();
  10. foreach($users as $u){
  11. echo $u;
  12. }
  13. ?>
insert this into your page after the div:
  1.  
  2. 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.
Last edited by digital-ether : Mar 15th, 2008 at 1:56 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC