Forum Moderators: open

Message Too Old, No Replies

Ajax Problem XMLHttpRequest

         

mdurrant

1:21 am on Mar 23, 2008 (gmt 0)

10+ Year Member



I'll start off by saying I'm very new to Ajax, so any help would be appreciated and I apologize if the answer is obvious. I'm having a problem with the following code. What I'd like to happen is the link "Update" is clicked and it fetches update.php?cid= and does a little loading thing while it loads that php script, and then prints the success/error message back. Right now it locks up on the loading part.

<script type="text/javascript">
var xmlHttp=null;

function doUpdate(str,elmid)
{
if (str.length==0)
{
document.getElementById("upnotice"+elmid).innerHTML="";
return;
}
try
{// Firefox, Opera 8.0+, Safari, IE7
xmlHttp=new XMLHttpRequest();
}
catch(e)
{// Old IE
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert ("Your browser does not support XMLHTTP!");
return;
}
}
var url="update.php?cid=" + str;
xmlHttp.open("GET",url,false);
xmlHttp.onreadystatechange = parseInfo(elmid);
xmlHttp.send(null);
}

function parseInfo(elmid)
{
if(xmlHttp.readyState == 1)
{
document.getElementById("upnotice"+elmid).innerHTML = '<img src="/images/ajax.gif" border="0" style="vertical-align: middle" /> Updating';
}
if(xmlHttp.readyState == 4)
{
var answer = xmlHttp.responseText;
document.getElementById("upnotice"+elmid).innerHTML = answer;
}
}
</script>

<form>
<div id="upnotice2"><a href="javascript:doUpdate('1',2)">Update 1</a></div><br />
<div id="upnotice3"><a href="javascript:doUpdate('2',3)">Update 2</a></div>
</form>

daveVk

2:22 am on Mar 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



xmlHttp.onreadystatechange = parseInfo(elmid);

should be

xmlHttp.onreadystatechange = parseInfo;

The original will execute parseInfo(elmid) and assign the returned value
to xmlHttp.onreadystatechange.

elmid will no longer be passed to parseInfo. At least for starters make elmid global;