fortiz147 0 Newbie Poster

please help this simple script doesn't work. it's suppose to display the word failed in the ajaxDiv section of the index.php script when the login fails.

index.php

<?session_start(); 
if(isset($_SESSION['username']))

{
header("location:home.php");

}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="ajax.js" type="text/javascript"></script>

<LINK REL=StyleSheet HREF="css/style.css" TYPE="text/css">
<link REL="SHORTCUT ICON" HREF="images/sm.ico">
</head>

<body>
<form name="login" method="post">


	<TABLE align="center" >
	
		<TR>

			<TD  BACKGROUND="images/ums2.jpg" height="560" width="800px">
			
			<div id="tab">
			
			<p align="center"><strong><font color="#ffffff" face="tahoma" size="1">username:</font></strong>
      		<font color="#000000"> 
      		<input type="text" name="username" size="18">
   			</font></p>
   			<p align="center"><font color="#ffffff" face="tahoma" size="1"><strong>password:</strong></font>
            <font color="#000000"> 
            <input name="password" type="password" size="18"> 
            </font></p>
        
            <p align="center"> 
            <input type="button" onclick='ajaxFunction()' name="Submit" value="log in">
            </p>

            </div>
    
            <div id='ajaxDiv'>display error here</div>

   			<p align="center"> <font color="#01b4fe">
   			
			
			

			</font></p> 
   		    </div>
            
    		</TD>
    
    	</TR>
    
    </TABLE> 
	
</form>
</body>
</html>

login_code.php

<?session_start();
include("conn.php");
$u=$_GET['username'];
$p=md5($_GET['password']);
$sql = "select * from login where username='$u' and password='$p'";
$result = mysql_query($sql);
$row=mysql_fetch_array($result);

if (mysql_num_rows($result)!= 1)
{ 
echo "failed";
} 
else if ($row[3]=='system administrator')
{ 
 $_SESSION['x'] = "1";
 $_SESSION['username'] = "$u";
 $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
 header("location:home.php"); 
}
else if ($row[3]=='project manager')
{
 $_SESSION['x'] = "2";
 $_SESSION['username'] = "$u";
 $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
 header("location:home.php");
}
else
{
echo "only system administrators and project managers are allowed to view this site";
}
?>

ajax.js

//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var u = document.getElementById('username').value;
	var p = document.getElementById('password').value;

	var queryString = "?username=" + u + "&password=" + p;
	ajaxRequest.open("GET", "login_code.php" + queryString, true);
	ajaxRequest.send(null);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.