再说一次,我有一些对你们来说可能很简单的东西,但我得到了
syntax error <?php // login.php line one.
然后在我得到的javascript上
ReferenceError: ajax is not defined
ajax.onreadyState = function(){
我看了很久,想不通。我知道这只是我没有放入的愚蠢的东西,但对于我的生活,我不知道。如果你不能说我是新来的,哈哈。
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<form action="login.php" method="post" id="loginForm">
<fieldset>
<legend>Login</legend>
<div><label for="email">Email Address</label><input type="email" name="email" id="email" required></div>
<div><label for="password">Password</label><input type="password" name="password" id="password" required></div>
<div><label for="submit"></label><input type="submit" value="Login →" id="submit"></div>
</fieldset>
</form>
<script src="js/login.js"></script>
<script src="resources/login.php"></script>
<script src="js/ajax.php"></script>
</body>
</html>
登录.js
function validateForm() {
'use strict';
var email = document.getElementById('email');
var password = document.getElementById('password');
if ( (email.value.length > 0) && (password.value.length > 0)) {
var ajax = getXMLHttpRequestObject();
return true;
} else {
alert('Please complete the form!');
return false;
}
}
function init() {
'use strict';
if (document && document.getElementById) {
var loginForm = document.getElementById('loginForm');
loginForm.onsubmit = validateForm;
}
}
window.onload = init;
ajax.onreadyState = function(){
if (ajax.readyState == 4) {
if ( (ajax.status > 200 && ajax.status < 300)
|| (ajax.status == 304) ) {
if (ajax.responseText == 'VALID') {
alert('You are now logged in!');
} else {
alert('The submitted values do not match those on file!');
}
} else {
document.getElementById('theForm').submit();
} // End of status IF-ELSE.
} // End of readyState IF.
ajax.open('POST', 'resources/login.php', true);
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var data = 'email=' + encodeURIComponent(email.value) + '&password=' + encodeURIComponent(password.value);
ajax.send(data);
return false;
}; // End of function assignation.`
登录.php
<?php // login.php
if ( isset($_POST['email'], $_POST['password'])
&& ($_POST['email'] == 'test@example.com')
&& ($_POST)['password'] == 'securepass') ) {
ehco 'VALID';
} else {
echo 'INVALID';
}
?>