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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A variable with the callback 1

Status
Not open for further replies.

KingSlick

Programmer
Mar 9, 2007
45
0
0
US
I have a small script to update a page using AJAX. Basically, it is to update the information of the customer when the blur off a text field. The question is, I have about 10 fields that could possibly be updated. So, is it possible to write one function that would work for all of the fields? The code below is what I have so far...

Code:
function updateCustomer(cid,field) {
	var theValue = document.getElementById(field + "V").value;
	
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		
    searchReq.open("GET", 'updateCustomer.php?id=' + cid + '&field=' + field + '&value=' + theValue, true);
    alert("The value is: " + theValue);
		searchReq.onreadystatechange = updateCustomerCB(field);
    searchReq.send(null);
		}	
}

function updateCustomerCB(div) {
	alert(div + "Div");
	if (searchReq.readyState == 4) {
    var ss = document.getElementById(div+ "Div");
    ss.innerHTML = '';
    var code = searchReq.responseText;
		ss.innerHTML = code;
  }
}
The first function works fine. All of the data gets sent, and the .php page receives the variables fine. Within the callback, the alert also works fine. The code after the alert is what seems not to be working. All of the other AJAX functions are also working well, are written in about the same manner. The only exception is the variable in the "var ss = document.getElementById(div+ "Div");" part. All of the other functions have "var ss = document.getElementById("DivName");". So is there something that I am doing wrong? or this just can't be done?

Thanks in advance
SM
 
Change this line:
Code:
searchReq.onreadystatechange = updateCustomerCB(field);
to this:
Code:
searchReq.onreadystatechange = function() { 
  updateCustomerCB(field);
}

Alternatively, you could assign the value of field to a global variable that can be seen by the updateCustomer and updateCustomerCB functions and update that way. Not so elegant, but it would do the job also.

Greg
 
Not a problem.

Glad to help, and thanks for the star! :)

Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top