我已经看了 1000 遍了。我不明白为什么它不起作用。
这是一个简单的 ajax 登录表单。即使凭据正确,它也会返回 false。(我通过直接发布到它来测试 ajax_login.php)
阿贾克斯功能:
$(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>