dsj2014
2010-03-23 19:21
浏览 62
已采纳

使用ajax登录表单,无需刷新即可提交

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";
    }
}
?>

图片转代码服务由CSDN问答提供 功能建议

我正在使用本教程创建登录表单 http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form /

它对我们的ldap服务器进行身份验证。

我遇到的问题是行

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

即使我们甚至不知道用户名和密码是否成功绑定,它仍会成功运行。

有没有办法 只有在发布的用户名和密码成功绑定后才能成功运行?

编辑:

添加了ldap-login.php的php代码

 <代码>&LT; PHP 
 
ini_set( '的display_errors',1); 
error_reporting(E_ALL); 
require_once( '/网络/ ee_web /包含/ adLDAP.php');  
 $ adldap = new adLDAP(); 
if(isset($ _ POST ['username'])&amp;&amp; isset($ _ POST ['password'])){
  $ username = $ _POST ['username']; 
 $ password = $ _POST ['password'];  //关联密码
 
 //连接到ldap服务器
 $ authUser = $ adldap-&gt; authenticate($ username,$ password); 
 if($ authUser === true){
 $ _SESSION [  'user_session'] = $ username; 
 if(isset($ _ SESSION ['user_session'])){
 print“ok”; 
} 
} 
其他{
 print“用户身份验证失败”;  
} 
} 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • drrkgbm6851 2010-03-23 19:27
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongpo1599 2010-03-23 19:27

    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();});
      }
    }
    
    评论
    解决 无用
    打赏 举报
  • dongshi2836 2010-03-23 19:33

    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.

    评论
    解决 无用
    打赏 举报
  • doulang5323 2010-03-23 19:33

    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();});
          }
       }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题