我已经查看了一千遍,但仍然不明白为什么它不能工作。
这是一个简单的ajax登录表单。即使凭证正确,它也返回false。(我通过直接提交到 ajax_login.php 进行了测试)
Ajax函数:
$(document).ready(function() {
$("#submit_login").click(function() {
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
type: "POST",
url: "scripts/ajax_login.php",
data: "username=" + username + "&password=" + password,
success: function(result) {
if(result == '0')
{
$(document.location = 'index.php? page=profile&user=' + username);
}
else
{
$("#login_error").show("fast");
}
}
});
return false;
});
});
ajax_login.php
session_start();
require('../conf/config.php');
if($_POST)
{
$u = mysql_real_escape_string($_REQUEST['username']);
$p = md5(mysql_real_escape_string($_REQUEST['password']));
$con = mysql_connect($mysql_server,$mysql_user,$mysql_password);
$db = mysql_select_db($mysql_db);
$query = mysql_query("SELECT * FROM users WHERE username = '$u' AND password = '$p'");
if(mysql_num_rows($query) > 0)
{
$sid = session_id();
$query = "UPDATE users SET sid='$sid' WHERE username='$u'";
$result = mysql_query($query);
if($result)
{
$_SESSION['loggedin'] = $u;
echo '0';
}
}
}
表单:
<div class="login_form">
<form action="scripts/ajax_login.php" method="POST">
Login<br /><br />
Username: <br />
<input type="text" name="username" id="username" /> <br />
Password: <br />
<input type="password" name="password" id="password" />
</div>
<input type="checkbox" name="remember" id="rem" value="checked"/><label for="rem">Stay logged in?</label><br />
<?php $formKey->outputKey(); ?>
<input type="submit" value="Login!" name="submit" id="submit_login" />
</form>