我的jquery-ajax代码出了什么问题,所以它运行不正常?

时间:2013-05-29 07:03:21

标签: php jquery smarty

以下是来自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文件在操作属性中使用了一个名为login的名称,其代码如下:

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

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

2 个答案:

答案 0 :(得分:2)

在php中, - &gt; login_error

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

在ajas回调中---&gt; log_error

      var log_error = response.log_error;

尝试将所有log_error更改为login_error

答案 1 :(得分:-1)

我相信$ .ajax里面的$(this)并没有指向你想要它的$ object(form)。

尝试写下这一行:

$('#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(),