| | |
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
![]() |
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.
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?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<span class="ad_notxt"><code class="inlinecode"><?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> "; for($i = 0; $i < $indx; $i++) { $name = explode(' ', $chatnames[$i], 2); echo " $name[0]"; if ($i < $indxcom) { echo ", "; } } } ?> </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
W. C. Fields
I found A few Errors But Still no output, Help?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<span class="ad_notxt"><code class="inlinecode"> <? 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(\","\)"; } } } ?> </code></span>
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
W. C. Fields
using ajax to call a php fuction:
put this where you want resutls:
include this ajax library in the page you want the results to display on.html Syntax (Toggle Plain Text)
<div id="results" style="display:inline; width:100px; height:20px;"></div>http://www.codeproject.com/KB/ajax/SAL/sal_src.zipcreate php page with your function in it and do this:html Syntax (Toggle Plain Text)
<script type="javascript" src="path_to_ajax_lib"></script>
insert this into your page after the div:php Syntax (Toggle Plain Text)
<?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; } ?>
javascript Syntax (Toggle Plain Text)
SetInnerHTMLFromAjaxResponse('your_php_page.php?results=membernames', 'results');
Last edited by hunkychop; Mar 13th, 2008 at 5:58 pm. Reason: fix code tags
toast
•
•
•
•
using ajax to call a php fuction:put this where you want resutls:
include this ajax library in the page you want the results to display on.html Syntax (Toggle Plain Text)
<div id="results" style="display:inline; width:100px; height:20px;"></div>http://www.codeproject.com/KB/ajax/SAL/sal_src.zipcreate php page with your function in it and do this:html Syntax (Toggle Plain Text)
<script type="javascript" src="path_to_ajax_lib"></script>
insert this into your page after the div:php Syntax (Toggle Plain Text)
<?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; } ?>
javascript Syntax (Toggle Plain Text)
SetInnerHTMLFromAjaxResponse('your_php_page.php?results=membernames', 'results');
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)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Inserting dynamic values into Javascript variables (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Substitute for clip Property
- Next Thread: LogicWeb Javascript
| Thread Tools | Search this Thread |
ajax ajaxexample ajaxjspservlets array browser bug captcha captchaformproblem cart checkbox child class close codes cookies createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine enter events explorer ext file firefox form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe images internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl maps marquee masterpage math matrixcaptcha media menu object onerror onmouseoutdivproblem onreadystatechange parent paypal pdf php position post programming progressbar prototype rated redirect runtime safari scale scriptlets scroll search security session shopping size software star stars synchronous toggle unicode variables web webservice wysiwyg \n






