0

我已经使用 PHP,MySql,bootstarp,Jquery,表单验证插件和 AJAX 制作了我的登录表单,并且我的表单非常适合验证,我还成功制作了 2 个 php 脚本,使用 AJAX 检查数据库中用户登录电子邮件和密码的存在,但是如果我想将我在 2 个单独的 AJAX 文件中所做的两个 AJAX 响应都作为表单验证插件中的规则,如何添加 ajax 作为电子邮件存在目的的规则?

任何帮助表示赞赏?

这是我的代码

      <script type="text/javascript">
            /*function CheckEmail()
                {
                    fdata = $("#form1").serialize();
                    $.ajax({
                            type: "POST",
                            url: "CheckLogin.php",
                            data: fdata,
                            success: function(response)
                                                        { 
                                                            $("#LogErrmsg").addClass('ajaxerror').html(response);
                                                        }
                          });
                }
                function checkpass()
                {
                    fdata2 = $("#form1").serialize();
                    $.ajax({
                            type: "POST",
                            url: "Checkpass.php",
                            data: fdata2,
                            success: function(response)
                                                        { 
                                                            $("#LogErrmsg2").addClass('ajaxerror').html(response);
                                                        }
                          });

                }*/
    </script>

    <!-- Validate Plugin -->
 <script src="js/jquery.validate.min.js"></script>
 <style type="text/css">
        label.valid {
                     width: 24px;
                     height: 24px;
                     background: url(bootstrap/img/valid.png) center center no-repeat;
                     display: inline-block;
                     text-indent: -9999px;
                    }
        label.error {
                    font-weight: bold;
                    color: red;
                    padding: 2px 8px;
                    margin-top: 2px;
                    }
        form        {
                    position: relative;
                    margin: 15px;
                    padding: 5px 15px 5px 15px;
                    background-color:#fff;
                    border: 1px solid #ddd;
                    -webkit-border-radius: 4px;
                    -moz-border-radius: 4px;
                    border-radius: 4px;

                    }
    .ajaxerror      {
                    font-style:italic;
                    color:red;
                    }
</style>

<form id="form1" action="User_Login.php" method="post" class="form-horizontal">
    <fieldset>
      <legend>Members Login</legend>

      <div>
         <div class="control-group" >
             <div id="LogErrmsg" class="controls"></div>
             <div id="LogErrmsg2" class="controls"></div>
         </div>    
         <div class="control-group"><label class="control-label" for="email">Email :</label>
              <div class="controls"><input type="text" name="email" id="email" class="input-xlarge" onBlur="CheckEmail()"size="32"></div>
         </div>

         <div class="control-group"><label class="control-label" for="password">Password :</label>
              <div class="controls"><input type="password" name="password" id="password" class="input-xlarge" size="32" onBlur="checkpass()"></div>
         </div>

         <div class="control-group">
              <div class="controls"><input type="checkbox" name="remember" class="input-xlarge" id="remember" > Remember me </div>
         </div>

          <div class="form-actions">
            <div><button type="submit" class="btn btn-primary btn-large" >LogIn</button></div>
          </div>  

            <div class="controls"><a href="ForgotPassword.php?mode=password">Forgot Password</a></div>


            <div class="controls">Not a member yet! <a href="User_Register.php"> Register</a></div>

     </div>

    </fieldset>
    <script type="text/javascript">
                $(document).ready(function(){

                                 $('#form1').validate(
                                             {   

                                                 rules: {
                                                         email: {
                                                                 required: true,
                                                                 email: true


                                                             },
                                                         password: {

                                                                 required: true,

                                                                   }


                                                         },

                                                highlight: function(element) {
                                                                             $(element).closest('.control-group').removeClass('success').addClass('error');

                                                                             },
                                                success: function(element) {
                                                                             element
                                                                            .text('OK!').addClass('valid')
                                                                            .closest('.control-group').removeClass('error').addClass('success');
                                                                           }
                                                             });
                                        }); // end document.ready
  </script> 
</form>
4

1 回答 1

0
function checkEmail(){
   $.ajax({
      url : 'ajax-url.php',
      type : 'POST',
      dataType : 'text',
      cache: false,
      data: data,
      success : function(text){
         alert(text+' was returned');
         console.log('returned');
      },
      error : function(XMLHttpRequest, textStatus, errorThrown) {
         alert('Error '+errorThrown);
      }
   });
}
$("#email").on({keyup: function(){checkEmail();}, blur: function(){checkEmail();});

将 替换为alert()您正在处理字段的有效性。使用 Twitter Bootstrap,您可以将类添加success.control-group电子邮件字段的周围,为其提供绿色边框。

于 2013-05-28T12:53:39.343 回答