Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables within functions in javascript with Ajax, please hel

Status
Not open for further replies.

viktorlind

Programmer
Feb 27, 2009
1
SE
Hi guys.
I fell across these forums while looking for help in regards of passing variables. I found a thread that had exactly what I needed (thread216-1264994) however this doesnt work with my code and tbh I cant understand why it wouldnt work.

Here's my code:

Code:
function markCase(casenum) {
nocache = Math.random();
document.getElementById(casenum).style.display = "block";
document.getElementById(casenum).innerHTML = "<img src='media/spinner.gif'/>";
http.open('get', 'include/markCase.php?nocache='+nocache+'&case='+casenum);
http.onreadystatechange = function() { markCaseReply(casenum); };
}
function markCaseReply(casenum) {
if(http.readyState == 4){
	var response = http.responseText;
	document.getElementById(casenum).innerHTML = response;
	}
}

I am trying to pass a variable (casenum) from the first function in the ajax process to the second as I want the response to be put in the div called casenum.

Why doesnt this code work? What can I do to fix it?
The problem is that the variables isn't passed so the script halts at the second reply function.

Appreciate any help!
 
Hi

This works for me :
Code:
function markCase(casenum) {
nocache = Math.random();
document.getElementById(casenum).style.display = "block";
document.getElementById(casenum).innerHTML = "<img src='media/spinner.gif'/>";
[red]http=new XMLHttpRequest()[/red]
http.open('[red]GET[/red]', 'include/markCase.php?nocache='+nocache+'&case='+casenum);
http.onreadystatechange = function() { markCaseReply(casenum); };
[red]http.send(null)[/red]
}
function markCaseReply(casenum) {
if(http.readyState == 4){
    var response = http.responseText;
    document.getElementById(casenum).innerHTML = response;
    }
}
By the way, casenum in reality is not a number, right ?

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top