PDA

View Full Version : Help finding code errors, with AJAX



TimFA
05-11-2008, 10:21 PM
Hey guys,

The below script is all the JavaScript for my page to run, some things are messy and not finished because the site is not finished. Anyways, the AJAX seems to be having some problems. Whenever the function go() is ran the DIV request_status should display "Attempting to establish connection..." but it does not, telling me there is an error somewhere. But higher up functions like the image rollovers and faders work telling me they are not the cause. So that leaves the AJAX area. I'm going to post all the code just in case, I already ran it through JSLint but it didn't produce anything helpful. Any help is appreciated.

Activation area:


<form method=\"post\" action=\"?act=login\">
<input id=\"user\" type=\"hidden\" name=\"user\">
<input id=\"pass\" class=\"passbox\" type=\"password\" name=\"pass\">
<br>
<img id=\"loginbutton\" onClick=\"rollOver('loginbutton','login_click');go();\" onMouseOver=\"rollOver('loginbutton','login_over');\" onMouseOut=\"rollOver('loginbutton','login');\" src=\"resources/images/login.gif\">
</form>

(sorry about the messed up code, with the \'s but its reference from a PHP file so its needed.)

Code:


//Login functions

function selectUser(user) {
document.getElementById("login_users").style.display='none';
document.getElementById("login_pass").style.display='inline';
document.getElementById("user").value='' + user;
}

function backLogin() {
document.getElementById("login_users").style.display='inline';
document.getElementById("login_pass").style.display='none';
document.getElementById("user").value='';
}

//General image/text effects

function fadeIn(image) {
document.getElementById(image).style.opacity='1';
}

function fadeOut(image) {
document.getElementById(image).style.opacity='.5';
}

function rollOver(oldimage,newimage) {
document.getElementById(oldimage).src='resources/images/' + newimage + '.gif';
}

//Ajax request area.

xmlhttp=getHTTPObject();
function getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
xhr = false;
}
}
return xhr;
}
}

function RSchange() {
if (xmlhttp.readyState==1) {
document.getElementById('request_status').innerHTML="Sending data...";
}
else if (xmlhttp.readyState==4) {
document.getElementById('login_status').innerHTML=xmlhttp.responseText;
}
}
function go() {
var user=document.getElementById("user").value;
var pass=document.getElementById("pass").value;

var data="user=" + user + "&pass=" + pass;

xmlhttp.open("GET", "resources/login.php", true);
xmlhttp.onreadystatechange=RSchange;
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(null);

document.getElementById('request_status').innerHTML="Attempting to establish a connection...";
}


Thanks !
Tim.

Nile
05-12-2008, 02:56 AM
Sorry, I'm a newb at ajax, but shouldn't:


xmlhttp.open("GET", "resources/login.php", true);
Be:


xmlhttp.open("GET", "resources/login.php?"+data, true);

Sorry if this doesn't help.

TimFA
05-12-2008, 05:18 PM
I wish I could say I wasn't...well anyways thanks for giving it a shot, I'll try later. I tried to find help earlier no one ever came up with that idea so it might just work. Thanks !

Nile
05-12-2008, 10:41 PM
I wish I could say I wasn't...
Whats that supposed to mean?

TimFA
05-12-2008, 11:46 PM
A newb at Ajax lol. I was saying I wish I could say I was good at it, but I'm not.

thetestingsite
05-12-2008, 11:55 PM
Try this for your code:



//Login functions

function selectUser(user) {
document.getElementById("login_users").style.display='none';
document.getElementById("login_pass").style.display='inline';
document.getElementById("user").value='' + user;
}

function backLogin() {
document.getElementById("login_users").style.display='inline';
document.getElementById("login_pass").style.display='none';
document.getElementById("user").value='';
}

//General image/text effects

function fadeIn(image) {
document.getElementById(image).style.opacity='1';
}

function fadeOut(image) {
document.getElementById(image).style.opacity='.5';
}

function rollOver(oldimage,newimage) {
document.getElementById(oldimage).src='resources/images/' + newimage + '.gif';
}

//Ajax request area.

xmlhttp=getHTTPObject();

function getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
xhr = false;
}
}
return xhr;
}
}

function RSchange() {
if (xmlhttp.readyState==4) {
document.getElementById('login_status').innerHTML=xmlhttp.responseText;
}
else {
document.getElementById('request_status').innerHTML="Attempting to establish a connection...";
}
}
function go() {
var user=document.getElementById("user").value;
var pass=document.getElementById("pass").value;

var data="user=" + user + "&pass=" + pass;

xmlhttp.open("GET", "resources/login.php?"+data, true);
xmlhttp.onreadystatechange=RSchange;
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(null);
}


Hope this helps.

Nile
05-14-2008, 01:08 AM
I'm just curious what worked?

TimFA
05-15-2008, 03:25 PM
My host went down yesterday with HD failure so I have a bit to wait, they've been down for 8 hours now. o.o; I'll be sure to let you know which one works after I check Nile.