I want to working with login system jQuery ajax and json. I'm facing no problem with success function. I dn't know how to through custom errors like "incorrect login".

Here is my code, Please help me.

<script>
    $(document).ready(function(){
        $("#login").click(function(){
            if($("#defaultEmail").val() == ""){
                $("#defaultEmail").addClass("field-err");
                $("#defaultEmail").focus();
            }else if($("#defaultPassword").val() == ""){
                $("#defaultEmail").removeClass("field-err");
                $("#defaultPassword").addClass("field-err");
                $("#defaultPassword").focus();
            }else{
                $("#defaultPassword").removeClass("field-err");
                $('#loading').css('display', 'block');
                var email = $('#defaultEmail').val();
                var password = $('#defaultPassword').val();
                var queryString = "email=" + email + "&password=" + password + "";
                $.ajax({
                  url: 'secureLogin.php',
                  contentType: "application/json; charset=utf-8",
                  dataType:'json',
                  async: false,
                  type:'GET',
                  data: queryString,
                  success: function(data){
                    $('#loading').css('display', 'none');
                    var url = "c_profile.php";
                    $(location).attr('href',url);
                  },
                  error: function(request,error){
                    $('#loading').css('display', 'block');
                    $("#log-ajax-error").append("ERROR: " + error);
                  }
                });
            }
        });
    });
</script>

Here is my sucureLogin.php code

$email = $_GET['email'];
$password = $_GET['password'];
$encrypted_pass = md5($password);

    $sel = @mysql_query("SELECT `name`, `company`, `cid` FROM `company` WHERE `email` = '$email' AND `password` = '$encrypted_pass'");
    $count = @mysql_num_rows($sel);
    if($count > 0){
        while($data = @mysql_fetch_array($sel)){
            // Succes here
        }
    }else{
        $arr = array('a' => 'Unable to login');
        echo json_encode($arr);
    }

i'm throwing json_encode for the error. Please help me with this. Thanks in advance.

Recommended Answers

All 3 Replies

I think you might be confused with the success/error of the ajax call, and what you consider success/error in PHP. The result from PHP is returned in the data parameter in success.

The success event in ajax is called when the page returns something. If there is a server error (500), or the page cannot be found (404), then the error event is triggered.

As specified in the jQuery documentation, the error setting is...

A function to be called if the request fails.

This means it is only called if there is a problem requesting secureLogin.php, e.g. a HTTP error such as 500 or 404 etc.

Since your code isn't having trouble requesting secureLogin.php it will always be calling the success method. To do what you are trying to do, you can pass a 'success' flag with the data and check that within the success callback... for example:

// PHP may need tweaking as I am 
// not a PHP dev so this is just a guess!

$email = $_GET['email'];
$password = $_GET['password'];
$encrypted_pass = md5($password);

$sel = @mysql_query("SELECT `name`, `company`, `cid` FROM `company` WHERE `email` = '$email' AND `password` = '$encrypted_pass'");
$count = @mysql_num_rows($sel);
$data = [];

if($count > 0){
    $data = @mysql_fetch_array($sel);
    $data['success'] = true;
}else{
    $data['success'] = false;
    $data['error'] = 'Unable to login';
}

echo json_encode($data);

And then your success callback would be something like this:

success: function(data){
    if (data.success) {
        $('#loading').css('display', 'none');
        var url = "c_profile.php";
        $(location).attr('href',url);
    } else {
        $('#loading').css('display', 'block');
        $("#log-ajax-error").append("ERROR: " + data.error);
    }
},

Thanks guys, This code is worked for me.

On the server side

$arr = array('error' => 'Unable to login');
echo json_encode($arr);

On the client side

success: function(data){
    $('#loading').hide(); 
    if( 'error' in data ){ 
        $("#log-ajax-error").append("ERROR: " + data.error);
    }
    else{
       var url = "c_profile.php";
       $(location).attr('href',url);
    }
},

The error callback will be called if there's an error with the ajax request, so your code needs to be in the success handler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.