I want to use AJAX to process a simple login form. I thought it'd be pretty easy, but I just can't get it all to work.
index.php
<html>
<head>
<title>AJAX Login</title>
<script type="text/javscript">
var XMLHttpRequestObject = false;
if(window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function logIn() {
if(XMLHttpRequestObject) {
var obj = document.getElementById("show");
XMLHttpRequestObject.open("GET", "login.php");
XMLHttpRequestObject.onreadystatechange = function() {
if(XMLHttpRequestObject.readyState == 4
&& XMLHttpRequestObject.status == 200) {
obj.innerHtml = XMLHttpRequestObject.responseText;
}
}
XMLHttpRequestObject.send(null);
}
}
</script>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="username" name="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" \ /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="login" onclick="logIn();" /></td>
</tr>
</table>
</form>
<div id="show">should go here</div>
</body>
</html>
login.php
<?php
$username = "andrew";
$password = "andrew";
if($_POST['username'] != "") {
if($_POST['password'] != "") {
if(($_POST['username'] == $username) && ($_POST['password'] == $password)) {
echo "Login Success!";
}
else {
echo "Login Failure!";
}
}
else {
echo "You didn't enter a password";
}
}
else {
echo "You didn't enter a username";
}
?>
When I click the "Login" button, nothing happens. :(