views:

286

answers:

4

I am using this tutorial to create a login form http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/

It authenticates against w/ our ldap server.

What I am having a problem with is the line

success: function(){
 $('form#submit').hide(function(){$('div.success').fadeIn();});

It runs success even though we don't even know if the username and password binded successfully.

Is there a way to only run success if the username and password posted was binded successfully?

EDIT:

Added php code of ldap-login.php

<?php

ini_set('display_errors',1);
error_reporting(E_ALL);
require_once('/web/ee_web/include/adLDAP.php');
$adldap = new adLDAP();
if (isset($_POST['username']) && isset($_POST['password'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];  // associated password

    // connect to ldap server
    $authUser = $adldap->authenticate($username, $password);
    if ($authUser === true) {
        $_SESSION['user_session'] = $username;
        if(isset($_SESSION['user_session'])) {
            print "ok";
        }   
    }
    else {
      print "User authentication unsuccessful";
    }
}
?>
A: 

Have your ajax function return whether or not the username and password binded successfully. Your code would then look like:

success: function(data){
  if(data.success){
    $('form#submit').hide(function(){$('div.success').fadeIn();});
  }
}
Peter
+1  A: 

If your response comes back having a simple string "ok" or the error message, you can check it this way:

success: function(data){
  if(data === "ok") {
    $('form#submit').hide(function(){$('div.success').fadeIn();
  } else {
    alert(data); // If not "ok", show error message
  }
});

Alternatively, send/check a more complex JSON response.

Nick Craver
how would I pass a string thru the data variable via php? I know I can set $data = "ok" within my conditional if bind was successful, but unsure how to feed it into that jquery function.
Brad
@Brad - Whatever the response text is, that's automatically the first parameter to the success function. If you just echo out "ok" or an error message, this approach will work. You can see the success method definition here: http://api.jquery.com/jQuery.ajax/
Nick Craver
@Nick - added the php code that I use to bind to ldap, I print "ok", sorry if that is too literal - will that work?
Brad
@Brad - It should - Sorry, I don't have a php setup at the moment to test...it's been a while since my last php project :)
Nick Craver
+1  A: 

The reason why it runs succes is because succes means that the ajax call was succesfull. That does not mean that it validated against your logic which you did not implement yet but as described in the other answers is should work. But now you know when the succes callback it called.

Chino
A: 

What you need to do is return a flag through the success function.

The success: function in your case represents the form being submitted successfully, not necessarily the authentication being successful. the success function can accept a string indicating anything you'd like as returned by the server.

See success(data, textStatus, XMLHttpRequest) section in the jQuery docs.

You could do:

   success: function(status){
      if(status === "loggedIn") {
         $('form#submit').hide(function(){$('div.success').fadeIn();});
      }
   }
KP