Reputation: 1135
I have a login system hat uses ajax and php. The form checks are done using javascript then the form data sent to php to checked against the data base. The php will return 1 if the user is not in the database. I have done this before and it has worked fine but here, 'NOTHING'.
JQuery version:
<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';
}
?>
The alert in the complete function of the Ajax isn't opening with anything.
Upvotes: 0
Views: 238
Reputation: 375
Call your ajax function with success, and error methods,
$.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);
}
});
Upvotes: 0
Reputation: 32921
You probably want to do e = parseInt(e)
. You're currently just alerting parseInt(e)
. '1' === 1
is false. echo 1
and echo '1'
both come back as a string from PHP.
Upvotes: 0
Reputation: 35973
try to use event success instead of complete:
$.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');
}
}
});
Upvotes: 1