Hello geniuses,
I'm working to develop a custom AJAX function that will allow me to submit a server call and get a response within a single line.
The method of executing the AJAX would be pretty straightforward. A function would be called that would process data submitted by the user in one form or another. We would then build a custom query to a PHP page that would do all of the data processing work and return something. We would then specify a DIV to dump the returned value into and finally we would make the call to our AJAX processing function. That method would look something like this:
Now, this code is fine, no trouble at all. I can even create the xmlhttp request just fine. The trouble comes with specifying the div that will receive the response. The rest of the code looks like this:Code:function createNewAssignment() { //GET ALL DATA FROM FORM //COMPOSE THE URL FOR OUR AJAX QUERY var ajaxQuery = "ajax.php?Action=CreateAssignment" + othervalues; //NAME THE DIV THAT WILL HAVE CONTENT DUMPED INTO IT var dumpDIV = "ResponseContainer"; //DO THE AJAX submitAJAX(ajaxQuery, dumpDIV); }
Code:var http_request = false; function submitAJAX(ajaxQuery, dumpDIV) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { // set type accordingly to anticipated content type //http_request.overrideMimeType('text/xml'); http_request.overrideMimeType('text/html'); } } else if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { alrt('Cannot create XMLHTTP instance'); return false; } http_request.onreadystatechange = getResponse(dumpDIV); http_request.open('GET', ajaxQuery, true); http_request.send(null); } function getResponse(dumpDIV) { if (http_request.readyState == 4) { if (http_request.status == 200) { result = http_request.responseText; if (result == '') { alert('Houston, we have a problem!') } else { document.getElementById(dumpDIV).innerHTML = result; } } else { alert('There was a problem with the request.'); } } }
Everything is cool until the function "getResponse." Even this function will work just fine, if I don't want to get the name of DIV sent to the function (if I were to take out "(dumpDIV)" from the function call and the function name and plug in the literal name of the DIV rather than a variable, it works.
So, my question is - why might the script not be working? Why can't I get variables into the "getResponse" function?
Thank you everybody, hope this is an easy one!
Mike




Bookmarks