• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Another Javascript Problem

BigDH01

Golden Member
I know I just posted one on Friday, but another issue with the same program....

I can't seem to get a response from my PHP UNLESS I add an alert(http.readyState) to my code.

<script language="Javascript" type="text/Javascript">


var http;
function getXmlHttpObject()
{
try
{
var xmlHttp = new XMLHttpRequest();
}
catch (e1)
{
try
{
var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e2)
{
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}




function login()
{
http=getXmlHttpObject();
var sURL="login.php";
http.open("POST", sURL, true);
http.onreadystatechange = function()
{
if(http.readyState == 4 || http.readyState == "complete")
{
if(http.status == 200)
{
alert(http.responseText);
var res = http.responseText;
alert(res);
if(res == "false")
alert("false");
}
}
}
var params = "username=" + encodeURI(document.getElementById('username').value) + "&password=" + encodeURI(document.getElementById('password').value) + "&ajax=true";
//http.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.send(params);
}


</script>

The above will do nothing. If I add alert(http.readyState) in my onreadystatechange function, I will see it progress from 1 to 4, an alert will display showing me the expected response, and then I'll get another "1" and another alert with my response. Without the alert(http.readyState), I get nothing. Any ideas?
 
Sounds like an asynchronous timing problem. Have you run it in firefox and check firebug and the error console?

I figured out the issue. It actually wasn't with javascript, it was with my form. I had the form attribute onsubmit to run that javascript, but even without action defined, the form gets submitted apparently. I modified the javascript to run with the action attribute, and it runs fine.
 
Back
Top