我的 AJAX 登录管理代码有什么问题?当我尝试使用我的用户名和密码登录时,它与 PHP 完美匹配,但我的 AJAX 代码根本无法工作!问题是什么?
(注意:我是 jQuery 和 AJAx 的新手)
我有 index.php 的代码:
<html>
<head><title>Login form</title></head>
<body>
<form id="myForm" action="testlogin.php" method="post">
username: <input type="text" name="username"> <br />
password: <input type="password" name="password"> <br />
<button id="submit">Login</button>
</form>
<div id="ack"></div>
<script type="text/javascript" src="jquery-2.0.2.js" ></script>
<script type="text/javascript" src="testajax.js"></script>
</body>
</html>
我有 testlogin.php 的代码:
<?php require_once("includes/connection.php"); ?> // starting the session.
<?php
$username = mysql_real_escape_string( $_POST["username"]);
$password = mysql_real_escape_string( ($_POST["password"]));
$sql = "SELECT * FROM users2 WHERE (username='$username' AND password='$password')";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
if($res === FALSE) {
die(mysql_error()); // TODO: better error handling
}
if( $row[0] > 0)
echo "Login Successful";
else
echo "Failed To Login";
?>
<?php include("includes/footer.php"); ?> //closing the session
最后我有这个 testajax.js 代码:
$ ("button#submit") .click(function() {
if ( $( "#username") .val() == "" || $( "#password") .val() == "" )
$("div#ack") .html("Please enter both username and password");
else
$.post ($("#myForm") .attr("action"),
$ ("#myForm :input") .serializeArryay(),
function(data) {
$("div#ack") .html(data);
});
$("#myForm") .submit(function) {
return false;
}
});