我有一个使用ajax和php的登录系统。表单检查是使用javascript完成的,然后将发送到php的表单数据与数据库进行检查。如果用户不在数据库中,php将返回1。我以前也这样做过,效果很好,但在这里,"什么都没有"。
JQuery版本:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
AJAX:
var pass = $('#pass').val();
var user = $('#user').val();
$.ajax({
url: 'func/check-login.php',
type: 'POST',
data:{user:user,pass:pass},
complete: function(e){
alert(parseInt(e));
if(e === 1){
$('#main_error').html('That username password combination is incorrect');
}else{
alert('Log user in');
}
}
});
PHP:
<?php
include '../DB.func/connect.php';
if(!empty($_POST)){
$_POST['user'] = mysql_real_escape_string($_POST['user']);
$_POST['pass'] = mysql_real_escape_string($_POST['pass']);
$username = $_POST['user'];
$password = $_POST['pass'];
if($username == ''){$user_err = 'You must insert tour username';}
if($password == ''){$pass_err = 'You must insert your passowrd';}
$query = mysql_query("SELECT * FROM user_admin WHERE user_name = '$username' OR email = '$username' AND password = '".md5($password)."'")or die(mysql_error());
if(mysql_num_rows != 0){
//login
echo '0';
}else{
echo 1;
}
}else{
echo '1';
}
?>
Ajax完整功能中的警报没有打开任何内容。
尝试使用事件成功而不是完成:
$.ajax({
url: 'func/check-login.php',
type: 'POST',
data:{user:user,pass:pass},
success: function(e){
alert(parseInt(e));
if(e === 1){
$('#main_error').html('That username password combination is incorrect');
}else{
alert('Log user in');
}
}
});
您可能想要执行e = parseInt(e)
。您当前正在提醒parseInt(e)
。'1' === 1
为假。echo 1
和echo '1'
都是从PHP返回的字符串。
使用成功和错误方法调用ajax函数,
$.ajax({
url: 'func/check-login.php',
type: 'POST',
data:{user:user,pass:pass},
success: function(e){
if( parseInt(e) === 1){
$('#main_error').html('That username password combination is incorrect');
}else{
alert('Log user in');
}
},
error: function(jqXHR, textStatus, errorThrown)
{
//for checking error console these variables
console.log( jqXHR);
console.log( textStatus);
console.log( errorThrown);
}
});