Ajax & IE problem.
Posted: Wed Jan 31, 2007 7:15 pm
Javascript part:
log_in.php:
Now, everything works on firefox and opera, when logIn() is called everything goes as planned. But in IE when logIn() is called the first time, every thing goes as planned except for the second and thereafter time(s).
So lets say I input my username, hit submit then calling the function logIn(...), sending a request to the server and the server finds that I only gave my username and that my password was not entered then exiting the php script with the exit(...) function, sending back a message saying how I need to enter my password.
So then I say "Okay" then enter my password and hit submit. Now what happens in internet explorer 6 is my information is sent, session variables are set but the part in my js file:
doesn't appear to execute. And if I refresh the page I'm logged in so I know that the session variables have been set, it's just the Javascript part that poses a problem.
So, why is it that the second time logIn() is executed in IE6 the piece of Javascript code does not get parsed? And why only in IE6 does this happen?
Thanks for taking the time on read this post, appreciate it.
Code: Select all
var HttpRequest;
try
{
// Firefox, Opera 8.0+, Safari
HttpRequest = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
}
}
}
function logIn(username, password)
{
HttpRequest.onreadystatechange = function ()
{
if(HttpRequest.readyState == 4 && HttpRequest.status == 200)
{
if (HttpRequest.responseText)
{
document.getElementById('log_in_errors').innerHTML = HttpRequest.responseText;
}
else
{
document.getElementById("log_in_form").style.display = 'none';
document.getElementById("log_in").style.borderBottom = "1px solid #cbcbcb";
document.getElementById('my_account').style.display = '';
document.getElementById('log_out').style.display = '';
document.getElementById('sign_up').style.display = 'none';
document.getElementById('log_in').style.display = 'none';
}
}
}
document.getElementById('log_in_errors').innerHTML = '';
HttpRequest.open('POST', 'log_in.php', true);
HttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
HttpRequest.send("username=" + encodeURI(username) + "&password=" + encodeURI(password));
}
function logOut()
{
HttpRequest.onreadystatechange = function ()
{
if(HttpRequest.readyState == 4 && HttpRequest.status == 200)
{
document.getElementById('my_account').style.display = 'none';
document.getElementById('log_out').style.display = 'none';
document.getElementById('sign_up').style.display = '';
document.getElementById('log_in').style.display = ''
}
}
HttpRequest.open('POST', 'log_out.php', true);
HttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
HttpRequest.send(null);
}
Code: Select all
<?
if ((!$_POST[username]) || (!$_POST[password])) {
exit("Username or password was not entered.");
}
$db_name = "";
$table_name = "";
$connection = @mysql_connect("", "", "") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT * FROM $table_name WHERE username = '$_POST[username]' AND password = '$_POST[password]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
$num = mysql_num_rows($result);
if ($num != 0) {
$row = mysql_fetch_array($result);
if ($row[active] == 1) {
//start a session
session_start();
//asign the value of the username and password to two superglobals
$_SESSION[username] = ucwords(strtolower($_POST[username]));
$_SESSION[password] = ucwords(strtolower($_POST[password]));
$_SESSION[logged] = true;
} else {
include("functions.php");
Table ("<b>Your account has not been activated.</b><BR><br>Please active your account by following the instructions provided in the email that should have been sent to you.<br><BR>If you did not receive such an email.","width='100%' height='100%' align=center");
exit;
}
} else {
exit("Incorrect username or password.");
}
?>
Now, everything works on firefox and opera, when logIn() is called everything goes as planned. But in IE when logIn() is called the first time, every thing goes as planned except for the second and thereafter time(s).
So lets say I input my username, hit submit then calling the function logIn(...), sending a request to the server and the server finds that I only gave my username and that my password was not entered then exiting the php script with the exit(...) function, sending back a message saying how I need to enter my password.
So then I say "Okay" then enter my password and hit submit. Now what happens in internet explorer 6 is my information is sent, session variables are set but the part in my js file:
Code: Select all
document.getElementById("log_in_form").style.display = 'none';
document.getElementById("log_in").style.borderBottom = "1px solid #cbcbcb";
document.getElementById('my_account').style.display = '';
document.getElementById('log_out').style.display = '';
document.getElementById('sign_up').style.display = 'none';
document.getElementById('log_in').style.display = 'none';
So, why is it that the second time logIn() is executed in IE6 the piece of Javascript code does not get parsed? And why only in IE6 does this happen?
Thanks for taking the time on read this post, appreciate it.