/    Sign up×
Community /Pin to ProfileBookmark

combination javascript and php (AJAX matter)

Okay… the purpose of this is to run a script that sets a user to “online” and then gets all the other usernames that are also set to online. This problem is focused around AJAX (i think), as i cant seem to get the function to even activate to start the process.
main control file:

[CODE]<?php
$username=$_GET[n];
?>
<html>
<head>
<script src=”onlineupdate.js” type=”text/javascript”>
</script>
<script type=”text/javascript”>
function doupdate()
{
var v= ‘<?=$username;?>’;

setTimeout(“getonline(‘<?=$username;?>’)”,50);
setTimeout(“doupdate()”,800);
}
setTimeout(“doupdate()”,100);
</script>
</head>

<p>

<div id=”showonline”><b>List is loading…</b></div>
</p>
<body bgcolor=”#000000″>
</body>
</html>[/CODE]

onlineupdate.js:

[CODE]var xmlHttp
function getonline(str)
{
alert(“Loaded script”);
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert (“Browser does not support HTTP Request”)
return
}
var mytime= &ms=+new Date().getTime();
var url=”online.php?n=”+str+mytime ;
xmlHttp.open(“GET”,url,true);
xmlHttp.onreadystatechange=stateChanged ;

xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState==”complete”)
{
document.getElementById(“showonline”).innerHTML=xmlHttp.responseText
}
}

function GetXmlHttpObject()
{
var xmlHttp = null;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch (e)
{
if ( !GetXmlHttpObject.failedDetection )
{
alert(“Your browser does not support AJAX.”);
GetXmlHttpObject.failedDetection = true;
}
}
}
}
return xmlHttp;
}
GetXmlHttpObject.failedDetection = false;[/CODE]

The above javascript takes the argument, and ads it to the url, so that the target knows the username to keep setting “online”. As it is right now, the alert statement at the top of the script saying “Loaded script” isn’t even working…which lets me know that this function is not being executed from the main page.

online.php:

[CODE]<?php

include “connect.php”;
$username=$_GET[n];
$setonline=mysql_query(“UPDATE usercodes SET usonline=1 WHERE username=’$username'”) or die(mysql_error());

$getmsg2=mysql_query(“SELECT username from usercodes where usonline=1″) or die(mysql_error());

while($getmsg3=mysql_fetch_array($getmsg2))

{
if ($getmsg3[name]==”stargateanubis14”)
{
print “<font color=’green’><b>$getmsg3[name]</b></font>”;
}
else
{
print “<font color=’red’><b>$getmsg3[name]</b></font>”;
}
}

?>
<br>
<br>
<br>
<body bgcolor=#000000>
[/CODE]

The above php code simply takes the user’s name from the url, sets that user to “online”, and then prints out all the users who are online too.

When i view the source, it shows the function having the correct username in place, but the function just wont activate.
I even attempted to run it without the variable/argument, and it didnt work.
I hate making people proof read like this…but i cant figure this out.
I am fairly new to AJAX in general, so any help or ideas are gratefully accepted!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyOct 28.2008 — The best thing to do is to get an interactive debugger and step through the AJAX initialization and subsequent execution to see where it is falling apart. I just finished writing such an AJAX function with PHP to retrieve online users for the chatroom I'm working on. I know there are free scripts out there for this kind of stuff, but I didn't want all their bells and whistles -- so I wrote my own.

I use the MS Script Debugger with IE7 and the Venkman Script Debugger with Firefox.
Copy linkTweet thisAlerts:
@A1ien51Oct 28.2008 — HINT:
<i>
</i>var mytime = &amp;ms = +new Date().getTime();


Eric
×

Success!

Help @stargateanubis1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...