I'm using the basic AJAX setup that I've seen on most tutorials (with minor variations):
function createRequestObject() {
var tmpXmlHttpObject;
//depending on what the browser supports, use the right way to create the XMLHttpRequest object
if (window.XMLHttpRequest) {
// Mozilla, Safari would use this method ...
tmpXmlHttpObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// IE would use this method ...
tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
}
return tmpXmlHttpObject;
}
//call the above function to create the XMLHttpRequest object
var http = createRequestObject();
function makeGetRequest(phpfile) {
//make a connection to the server ... specifying that you intend to make a GET request
//to the server. Specifiy the page name and the URL parameters to send
http.open('get', phpfile);
//assign a handler for the response
http.onreadystatechange = processResponse;
//actually send the request to the server
http.send(null);
}
function processResponse() {
//check if the response has been received from the server
if(http.readyState == 4){
//read and assign the response from the server
var response = http.responseText;
//do additional parsing of the response, if needed
//in this case simply assign the response to the contents of the <div> on the page.
document.getElementById('container').innerHTML = response;
//If the server returned an error message like a 404 error, that message would be shown within the div tag!!.
//So it may be worth doing some basic error before setting the contents of the <div>
}
}
Then I'm making the request here:
<a href="javascript:makeGetRequest('call.php')">Click here</a>
This code works just fine.
But my problem is that with the current code any response has to be put in a div with id='container'.
To be more flexible I wanted to have the processResponse function take a parameter (divname) that I could replace with the hard coded 'container' div name.
But when I try and do that simply making this change to the JS:
function makeGetRequest(divname,phpfile) {
//make a connection to the server ... specifying that you intend to make a GET request
//to the server. Specifiy the page name and the URL parameters to send
http.open('get', phpfile);
//assign a handler for the response
http.onreadystatechange = processResponse(divname);
//actually send the request to the server
http.send(null);
}
function processResponse(divname) {
//check if the response has been received from the server
if(http.readyState == 4){
//read and assign the response from the server
var response = http.responseText;
//do additional parsing of the response, if needed
//in this case simply assign the response to the contents of the <div> on the page.
document.getElementById(divname).innerHTML = response;
//If the server returned an error message like a 404 error, that message would be shown within the div tag!!.
//So it may be worth doing some basic error before setting the contents of the <div>
}
}
And this change to HTML:
<a href="javascript:makeGetRequest('container', 'call.php')">Click here</a>
This does not work at all, the http.onreadystatechange is stuck at 1. This seems unbelievable to me I almost couldn't believe my eyes as I removed the variable to make it work again and then added it back to break it again.
In fact I noticed that even if I add parenthesis to the function call (NOT touching the definition OR adding paramenters) it would break:
function makeGetRequest(phpfile) {
//make a connection to the server ... specifying that you intend to make a GET request
//to the server. Specifiy the page name and the URL parameters to send
http.open('get', phpfile);
//assign a handler for the response
http.onreadystatechange = processResponse();
//actually send the request to the server
http.send(null);
}
The only difference in the original working code was adding the () to processResponse call!!
Anyways, this seems like I must be making some basic mistake as I am not that familiar with JS or AJAX (but respectable at HTML and PHP).
Any help is appreciated - thanks.