所以我是AJAX的新手。我目前正在使用带有PHP的ajax创建动态登录。如果凭证有效,则php函数返回1值,如果不是,则返回0值。但是,现在AJAX似乎永远无法在登录有效时提取值,即使系统实际上是在记录我。
AJAX
$(document).ready(function(){
// $("#add_err").css('display', 'none', 'important');
$("#login").click(function(){
username=$("#login-username").val();
password=$("#login-password").val();
$.ajax({
type: "POST",
url: "userMGMT/login.php",
dataType: 'html',
data: "name="+username+"&pwd="+password,
success: function(html){
if(html=='1') {
//document.write("hello");
$("#add_err").html("right username or password");
window.location="newgame.php";
}
else {
//document.write("hellefefo");
$("#add_err").css('display', 'inline', 'important');
$("#add_err").html("Wrong username or password");
}
},
beforeSend:function()
{
$("#add_err").css('display', 'inline', 'important');
$("#add_err").html("Loading...")
}
});
return false;
});
});
PHP
$username=$_POST['name'];
$password=$_POST['pwd'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql = "SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
echo '1';
session_start();
$_SESSION['loggedin'] = true;
$username = strtolower($username);
$_SESSION['username'] = $username;
header( 'Location: uid.php');
}
else
{
echo '0';
//header( 'Location: ../404.php?info=The System Couldnt Log You On');
}