/    Sign up×
Community /Pin to ProfileBookmark

i have a problem getting my AJAX to work

hi,

i am having a problem getting my AJAX to work…

what i want it to do is do the function on page load, the refresh the data every second, i have messed about with the code but cannot seem to get it to do what i want…it loads the function fine on page load, but it wont refresh…

can some one please help me with this.

[CODE]
<?php
include(‘connect.php’);
$con = mysql_connect(“$server”,”$user”,”$pass”);
if (!$con) {
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“$db”, $con);

$sql=”SELECT * FROM users WHERE accessid = “.$_GET[‘user’];

$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
?>
<body onload=”showDetails(<?php echo $row[‘username’]; ?>)” />

<script type=”text/javascript”>

var xmlHttp

function showDetails(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert (“Your browser does not support AJAX!”);
return;
}
var url=”userinfo.php”;
url=url+”?q=”+str;
url=url+”&sid=”+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==1)
{
document.getElementById(“txtHint”).innerHTML==”Loading”;
}
if (xmlHttp.readyState==4)
{
document.getElementById(“txtHint”).innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}
setTimeout(“showDetails(<?php echo $row[‘username’]; ?>)”, 100);
</script>
<span id=”txtHint”>
</span>
<?php
}
mysql_close($con);
?>
[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Feb 25.2009 — In your setTimeout function you set the number of milliseconds to 100 which equates to .1 seconds. You need to tack on one more zero for it to work every second. I am not 100&#37; sure if that is the problem.
Copy linkTweet thisAlerts:
@Vinny69authorFeb 25.2009 — thanks for that, unfortunately it still isnt refreshing my data ?

i just dont understand why it isn't working...
Copy linkTweet thisAlerts:
@Vinny69authorFeb 26.2009 — i put a php RAND() code in the data and i just figured out that the script is only refreshing the data once, i want it to continue refreshing so that the data is up to date...

i know this is possible as Windows live profiles use it, i just cannot figure it out
Copy linkTweet thisAlerts:
@Vinny69authorFeb 26.2009 — woooo, i got it working i was just fiddling around and its working now...

i just changed the following part of the code:
<i>
</i> }
}
return xmlHttp;
}
setTimeout("showDetails(&lt;?php echo $row['username']; ?&gt;)", 100);
&lt;/script&gt;


To this and its now working:
<i>
</i> }
}
setTimeout("showDetails(&lt;?php echo $row['username']; ?&gt;)", 100);
return xmlHttp;
}
&lt;/script&gt;



thankyou "skywalker2208" for your help
Copy linkTweet thisAlerts:
@Vinny69authorFeb 27.2009 — hi again,

how would i go about adding a bgsound when the data changes,

but to only play once onChange

i am using this script for my web messenger and i need it for when people sign in

i have th folowing statuses:

online

busy

away

offline

i only want the sound to play when the user signs in.

even if they sign in as away or busy.

but i dont want the sound to replay every time the user swiches between these statuses

thanks in advance for any help.



Vinny
×

Success!

Help @Vinny69 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...