0

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.

3 Answers 3

1

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');
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

thats it success works, whats the difference between success and complete and they the same sort of thing
Success : This event is only called if the request was successful (no errors from the server, no errors with the data). Complete: This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests. @PaulLedger
0

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.

Comments

0

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);
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.