I decided to finally make the plunge and teach myself AJAX - after getting one app to work, I copied and pasted my code (more or less) into a new app to tinker with it more and turn it into something useful, and it stopped working. I spent around 5 hours trying to trace exactly what the issue was, and that I hadn’t made some sort of stupid typo.
Essentially, in one .js file with my AJAX event handlers, responseText returns blank. That would seem to point to a problem with the file I’m sending my data to on the server side, but when I use a file that I know works, it still returns blank.
Code:
ajaxauth.js:
function createRequestObject(){
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} 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) {
alert('Cannot create XMLHTTP instance');
return false;
}
return http_request;
}
var http_request = createRequestObject()
function sendPass(formpass){
document.getElementById('autho').innerHTML = 'Verifying...';
var password = 'pass=' + formpass;
http_request.open('POST','ajaxauth.php', true);
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http_request.onreadystatechange = checkAuth;
http_request.send(password);
}
function checkAuth(){
if (http_request.readyState == 4) {
result = http_request.responseText;
document.getElementById('autho').innerHTML = result;
}
}
ajaxauth.php:
<?php
require './auth.dat'; //should NOT be in the /var/www tree in production!
if ($_POST['pass']==$protempass){ echo 'Authenticated as Pro-Tem! Close this window when you are done!<input type=hidden value='.$_POST['pass'].' name=pass>';}
elseif ($_POST['pass']==$adminpass){echo 'Authenticated as Admin! Close this window when you are done!<input type=hidden value='.$_POST['pass'].' name=pass>';}
else{echo '<form name="authorization" onSubmit="sendPass(authorization.pass.value)" method="POST"><input type="password" name="pass" size="32"><input type="submit" value="Authenticate"> Authentication failed!<br />';}
?>
These two blocks of code work great.
The next file does not.
editdb.js:
var http_editdb = createRequestObject()
function newSenator(){
var newsen = 'lastname=' + document.newsenator.lastname.value + '&firstname=' + document.newsenator.firstname.value + '&middleinitial=' + document.newsenator.middleinitial.value + '&year=' + document.newsenator.year.value + '&newsenator=1' + '&pass=' + document.authorization.pass.value;
document.getElementById('senators').innerHTML = document.getElementById('senators').innerHTML + 'Adding...<br />';
http_editdb.open('POST','ajaxauth.php', true);
http_editdb.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http_editdb.onreadystatechange = updateTable;
http_editdb.send(newsen);
}
function updateTable(){
if (http_editdb.readyState == 4) {
result = http_editdb.responseText;
document.getElementById('senators').innerHTML = result;
}
}
This code above typically points to a file called “ajaxeditdb.php”, but in this case I’m pointing the newSenator function at “ajaxauth.php” because I know that ajaxauth should return SOMETHING in responseText. I’ve stepped through and echoed every variable out of the javascript file with alert(); and it opens the function updateTable() and the readyState gets to 4 and everything seems peachy, but then the responseText is blank. My best guess is I’m missing something about the way I’m sending data with newSenator(), but I really can’t see how that would be the case.
Yes, the code is sloppy/nicked from various AJAX tutorials, and no, I don’t think I need to open a second XMLhttp object if these are both in the same page, but I’m pretty sure that those are unrelated to the problem. Anyone have any ideas to help track down what the issue is? Thanks in advance to anyone who takes the time to help me out.