-1

以下是我来自 Smarty 模板的 HTML 代码:

<div id="user-popup-login" title="Demo Test Packages">
  <div id="pop-up">
    <!--<div class="popup-right"><img src="images_new/close.gif" alt=" " align="right" width="29" height="29" /></div>
    <div class="clear"></div>-->
    <div class="error_msg" id="login_error" style="text-align:center; margin-top:5px;">

    </div>
    <div class="clear"></div>
    <div class="popup-left">
      <form name="user_login_form" id="user_login_form" class="login_box" method="post" action="{$site_url}login">
      <input type="hidden" name="referral_url" value="{$referral_url}" id="referral_url" />
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td colspan="2"><input name="user_name" id="user_name" class="user" type="text" value="E-mail" onblur="if(this.value=='') this.value='E-mail';" onfocus="if(this.value=='E-mail') this.value='';" />
            </td>
          </tr>
          <tr></tr>
          <tr>
            <td colspan="2"><input name="user_password" id="user_password" class="user" type="password" value="password" onblur="if(this.value=='') this.value='password';" onfocus="if(this.value=='password') this.value='';" />
            </td>
          </tr>
          <tr></tr>
          <tr>
            <td><input name="Submit" type="submit" class="login-btn" value="" /></td>
            <td><div id="registern"><a href="#" class="register-btn">Register Now</a></div></td>
          </tr>
          <tr>
            <td align="right" colspan="2"><a href="{$site_url}forgot_password.php?op=forget_password" class="forget">Forgot Password? </a> &nbsp; </td>
          </tr>
        </table>
      </form>
    </div>
    <!--<div class="popup-right"><img src="images_new/facebook-sign.gif" alt=" " width="156" height="229" /></div>-->
    <div class="clear"></div>
  </div>
</div>

以下是我的 jQuery 代码:

<script language="javascript" type="text/javascript">
$(function() {  
  $("#user-popup-login").dialog({ autoOpen: false }); 
  $( ".user-not-loggedin" )
    .click(function() {
    $( "#user-popup-login" ).dialog( "option", "width", 400 );
    $( "#user-popup-login" ).dialog( "option", "modal", true );
    $( "#user-popup-login" ).dialog( "open" );

    return false;
  });

  $( ".get-start" )
    .click(function() {
    $( "#user-popup-login" ).dialog( "option", "width", 400 );
    $( "#user-popup-login" ).dialog( "option", "modal", true );
    $( "#user-popup-login" ).dialog( "open" );

    return false;
  });     

  //This function is used to submit User Login form
  $('#user_login_form').live('submit', function() { 
     $('.login-btn').attr('disabled', 'disabled');
     show_request('#login_error');
      $.ajax({   
        type: $(this).attr('method'),
        url: $(this).attr('action'), 
        data:$(this).serialize(),
        dataType: 'json',
        success: function(response) { 
          $('.login-btn').removeAttr('disabled', 'disabled');
          var log_error = response.log_error;

          if(log_error=='yes') { 
            var error_msg      = response.error_msg; 
            var dialog_title   = 'Input Errors Found';                 
            var dialog_message = error_msg; 
            $("#login_error").html(error_msg);
          } else { 
            $("#login_error").html('');
            $( "#user-login" ).dialog('close');
            var suc_msg   = response.suc_msg; 
            var dialog_title   = 'Logged in Successfully';                 
            var dialog_message = suc_msg; 

            var $dialog = $("<div class='ui-state-success'></div>")
            .html("<p class='ui-state-error-success'>"+dialog_message+"</p>")
            .dialog({
               autoOpen: false,
               modal:true,
               title: dialog_title,
               width: 500,               
               buttons:{
               'OK': function() {
                $(this).dialog('close');
                 }
               }                               
            });
            $dialog.dialog('open'); 
            $('#user_login_form')[0].reset();  
          }

        } 
      });    
      return false;
  }); 
}); 
</script>

我的名为 user_login.php 的 php 文件有一个别名为登录名,用于操作属性,具有以下代码:

<?php
  include_once("includes/application-header.php");

    prepare_request();

  $obj_login = new Login();

  if($gSession->ReadFromSession('IS_LOGGEDIN') && $_GET['act'] !='logout') {
    header("location:".SITE_URL."home");
        die();
  } else {    
      if( "POST" == $_SERVER['REQUEST_METHOD'] ) { 
            $login_data = extract_array( $_POST , array("user_name", "user_password") );
            $user_id = $obj_login->DoUserLogin($login_data);

            if($user_id) {
                list($subscription_type, $days_left) = $gUsers->GetUserSubscriptionDetailsByUserID($user_id);

                if($gUsers->InitiateUserSession($user_id, $subscription_type, $days_left)) {
                    $gUsers->UpdateUserLoginHistory($user_id);
                    $reponse_data['login_error'] = 'no';
                    $reponse_data = json_encode($reponse_data);
                    echo $reponse_data;
                    die();
                }
            } else {
                $error_msg =  $obj_login->GetError();
                list($login_data) = prepare_response($login_data);
                $error_msg = '<b>We are unable to log you in for one of the following reasons:</b>
                                            </p>
                                            <ul>
                                            <li>Incorrect email or password (case sensitive)</li>
                                            <li>Cookies are not enabled on your browser</li>
                                            <li>You havent registered with entrance prime</li>
                                            <li>We have not activated your account</li>
                                            </ul><p></p>';
                $reponse_data['login_error'] = 'yes';
                $reponse_data['error_msg'] = $error_msg;
                $reponse_data = json_encode($reponse_data);
                echo $reponse_data;    
                die();
            }
      }

      // CODE FOR LOGOUT
      if(isset($_GET['act']) && $_GET['act']=='logout') {
            $gUsers->UpdateUserLogoutHistory();
          $obj_login->UserLogout();

          header("Location:".SITE_URL."home");
          die();
      }
  }
?>

我的问题是当我单击登录按钮而不填写任何值时,表单提交时没有任何错误,并在弹出窗口中显示“未定义”而不是向我显示错误。谁能帮我解决这个问题?

4

2 回答 2

2

在 php 中,--> login_error

$reponse_data['login_error'] = 'no';

在 ajas 回调中--->log_error

      var log_error = response.log_error;

尝试将所有 log_error 更改为 login_error

于 2013-05-29T07:27:00.200 回答
-1

我相信 $.ajax 中的 $(this) 并不指向您希望它指向的 $ 对象(形式)。

试着在这一行下面写:

$('#user_login_form').live('submit', function() {
    myform = $(this); // caching the jquery object to a local variable

然后将其写在下面的适当部分(替换您的代码):

$.ajax({   
        type: myform.attr('method'),
        url: myform.attr('action'), 
        data:myform.serialize(),
于 2013-05-29T07:14:14.980 回答