我无法找出我的代码中有什么问题,顺便说一下,浏览器中没有错误。问题是当 ajax 将数据传递给 login.php 时,登录 php 无法识别数据(用户名和密码)或 ajax 未正确发送数据,$_Session['username']未定义;
这是我的代码,
$(function(){
var user = $('#username').val();
var pass = $('#password').val();
$('#login').click(function(){
$.ajax({
type: 'GET',
url: "../ajax/login.php",
data: 'username='+user+'&password='+pass
}).success(function(data){
if(data == "Success"){
$('#error').html("Hello!");
}else{
alert(data);
$('#error').html("Hello!");
}
}).error(function(){
alert("An error occured.");
});
});
});
login.php 代码
<?php
include '../library/Session.php';
include '../library/connection/Connection.php';
include '../library/Data_Cleansing.php';
$session->LoginCheckSession();
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$username = $CleanData->stripAndSlash($username);
$password = $CleanData->stripAndSlash($password);
$sql = mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password'") or die(mysql_error());
if ($sql && mysql_num_rows($sql) == 1) {
while ($row = mysql_fetch_array($sql)) {
if ($username == $row['username'] && $password == $row['password']) {
$_SESSION['username'] = $username;
echo "Success";
}
}
} else {
echo "Failed";
}
?>
谁能给我一个关于如何解决这个问题的建议?当我试图提醒数据时,我得到的只是“失败”。警报(数据);