开发者

login form using ajax, to submit without refreshing

开发者 https://www.devze.com 2022-12-23 18:02 出处:网络
I am using this tutorial to create a login form http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/

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 = $开发者_C百科_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";
    }
}
?>


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.


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号