convert php function to ajax?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

convert php function to ajax?

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

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <span class="ad_notxt"><code class="inlinecode"><?php
  2.  
  3.  
  4. $chatnames = file('http://client11.addonchat.com/scwho.pl?id=292747&plain=1');
  5. $indx = count($chatnames);
  6. $indxcom = $indx - 1;
  7. if ($indx == 0) {
  8. echo "<b>No one is in the chat room at the moment</b>";
  9. }
  10. else{
  11. echo "<b>People Chatting:</b>&nbsp;&nbsp;";
  12. for($i = 0; $i < $indx; $i++) {
  13. $name = explode(' ', $chatnames[$i], 2);
  14. echo " $name[0]";
  15. if ($i < $indxcom) {
  16. echo ", ";
  17. }
  18.  
  19. }
  20. }
  21.  
  22. ?>
  23. </code></span>

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: convert php function to ajax?

 
0
  #2
Mar 13th, 2008
I found A few Errors But Still no output, Help?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <span class="ad_notxt"><code class="inlinecode">
  2. <?
  3.  
  4. Header("content-type: application/x-javascript");
  5. $chatnames = file('http://client11.addonchat.com/scwho.pl?id=292747&plain=1');
  6.  
  7.  
  8. $indx = count($chatnames);
  9. $indxcom = $indx - 1;
  10.  
  11. if ($indx == 0){
  12. echo "document.write(\"No one is in the chat room at the
  13.  
  14. moment"\)";
  15. }
  16.  
  17. else{
  18. echo "document.write(\"<b>Members Chatting:</b>"\)";
  19.  
  20.  
  21. for($i = 0; $i < $indx; $i++) {
  22.  
  23. $name = explode(' ', $chatnames[$i], 2);
  24.  
  25. echo "document.write(\"$name[0]"\)";
  26.  
  27. if ($i < $indxcom) {
  28.  
  29. echo "document.write(\","\)";
  30. }
  31.  
  32. }
  33. }
  34. ?>
  35.  
  36. </code></span>
  37.  
Last edited by Inny; Mar 13th, 2008 at 3: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 Quick reply to this message  
Join Date: Mar 2007
Posts: 55
Reputation: hunkychop is an unknown quantity at this point 
Solved Threads: 4
hunkychop's Avatar
hunkychop hunkychop is offline Offline
Junior Poster in Training

Re: convert php function to ajax?

 
0
  #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 5:58 pm. Reason: fix code tags
toast
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: convert php function to ajax?

 
0
  #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 Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: convert php function to ajax?

 
0
  #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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. 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 2: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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC