這是形式的HTML:登錄無法正常工作... PHP中的錯誤在哪裏?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<link rel="stylesheet" href="css/index.css" type="text/css" media="screen" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js" charset="utf-8"></script>
<script type="text/javascript" src="js/login.js" charset="utf-8"></script>
<script src="../htdocs/SpryAssets_index/SpryMenuBar.js" type="text/javascript"></script>
<link href="../htdocs/SpryAssets_index/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="includes/css/index.css" />
</head>
<body>
<div class="container">
<?php
include ('includes/header.html')
?>
<br /><br />
<h1>Login</h1>
<p id="results"></p>
<form action="login_ajax.php" method="post" id="login">
<p id="emailP">Email Address: <input type="text" name="email" id="email" /><span class="errorMessage" id="emailError">Please enter your email address!</span></p>
<p id="passwordP">Password: <input type="password" name="password" id="password" /><span class="errorMessage" id="passwordError">Please enter your password!</span></p>
<p><input type="submit" name="submit" value="Login!" /></p>
</form>
<div class="footer">
<?php
include ('includes/footer.html')
?>
</div>
</div>
</body>
</html>
這是處理login_ajax.php:
<?php
if (isset($_POST['email'], $_POST['password'])) {
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
function check_login($dbc, $email = 'email', $pass = 'pass') {
$errors = array(); //Intialize the error array.
if (empty($email)) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = filter_var($email, FILTER_SANITIZE_EMAIL);
}
if (empty($pass)) {
$errors[] = 'You forgot to enter your password.';
} else {
$p = mysqli_real_escape_string($dbc, trim($pass));
}
if (empty($errors)) {
$q = "SELECT first_name, last_name FROM users WHERE email='$e' AND pass=SHA1('$p')";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 1) {
$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
echo 'CORRECT';
} else {
$errors[] = 'The email address and password entered do not match those on file.';
echo 'INCORRECT';
}
session_start();//START A SESSION HERE.
$_SESSION['first_name'] = $data['first_name'];
$_SESSION['last_name'] = $data['last_name'];
}
}
check_login($dbc, $_POST['email'], $_POST['password']);
} else {
echo 'INVALID_EMAIL';
}
} else {
echo 'INCOMPLETE';
}
?>
這是JavaScript處理該回波login.js:
// Script 15.10 - login.js
// This script is included by login.php.
// This script handles and validates the form submission.
// This script then makes an Ajax request of login_ajax.php.
// Do something when the document is ready:
$(function() {
// Hide all error messages:
$('.errorMessage').hide();
// Assign an event handler to the form:
$('#login').submit(function() {
// Initialize some variables:
var email, password;
// Validate the email address:
if ($('#email').val().length >= 6) {
// Get the email address:
email = $('#email').val();
// Clear an error, if one existed:
$('#emailP').removeClass('error');
// Hide the error message, if it was visible:
$('#emailError').hide();
} else { // Invalid email address!
// Add an error class:
$('#emailP').addClass('error');
// Show the error message:
$('#emailError').show();
}
// Validate the password:
if ($('#password').val().length > 0) {
password = $('#password').val();
$('#passwordP').removeClass('error');
$('#passwordError').hide();
} else {
$('#passwordP').addClass('error');
$('#passwordError').show();
}
// If appropriate, perform the Ajax request:
if (email && password) {
// Create an object for the form data:
var data = new Object();
data.email = email;
data.password = password;
// Create an object of Ajax options:
var options = new Object();
// Establish each setting:
options.data = data;
options.dataType = 'text';
options.type = 'get';
options.success = function(response) {
// Worked:
if (response == 'CORRECT') {
// Hide the form:
$('#login').hide();
// Show a message:
$('#results').removeClass('error');
$('#results').text('You are now logged in!');
} else if (response == 'INCORRECT') {
$('#results').text('The submitted credentials do not match those on file!');
$('#results').addClass('error');
} else if (response == 'INCOMPLETE') {
$('#results').text('Please provide an email address and a password!');
$('#results').addClass('error');
} else if (response == 'INVALID_EMAIL') {
$('#results').text('Please provide your email address!');
$('#results').addClass('error');
}
}; // End of success.
options.url = 'login_ajax.php';
// Perform the request:
$.ajax(options);
} // End of email && password IF.
// Return false to prevent an actual form submission:
return false;
}); // End of form submission.
}); // End of document ready.
此表單從PHP處理程序返回一個INCOMPLETE,Javascript將返回「請提供電子郵件地址和密碼!」。
你做了什麼調試?我沒有看到任何。您至少應該能夠將其縮小爲代碼片段。 –
刪除用於PHP協助的'isset'和'@'。在這種情況下,通知和警告很有用。 – mario
我們可以看到您的表單的HTML代碼片段嗎? –