使用jQuery远程方法验证显示名称

时间:2016-02-08 08:09:09

标签: php jquery mysql

如果已经存在,我尝试添加远程方法来检查显示名称。

如果电子邮件已经存在,则电子邮件验证可以检查和显示消息,但显示名称验证不起作用。我的代码出了什么问题?

我的代码

register.php

<?php require_once 'config.php'; ?>
<?php 
    if(!empty($_POST)){
        try {
            $user_obj = new Cl_User();
            $data = $user_obj->registration( $_POST );
            if($data)$success = USER_REGISTRATION_SUCCESS;
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Smart Registration Form</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <link href="css/login.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </head>
  <body>
    <div class="container">
        <div class="login-form">
            <?php require_once 'templates/message.php';?>

            <h1 class="text-center">Smart Tutorials</h1>
            <div class="form-header">
                <i class="fa fa-user"></i>
            </div>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="form-register" role="form" id="register-form">
                <div>
                    <input name="name" id="name" type="text" class="form-control" placeholder="Dispaly Name"> 
                    <span class="help-block"></span>
                </div>
                <div>
                    <input name="email" id="email" type="email" class="form-control" placeholder="Email address" > 
                    <span class="help-block"></span>
                </div>
                <div>
                    <input name="password" id="password" type="password" class="form-control" placeholder="Password"> 
                    <span class="help-block"></span>
                </div>
                <div>
                    <input name="confirm_password" id="confirm_password" type="password" class="form-control" placeholder="Confirm Password"> 
                    <span class="help-block"></span>
                </div>
                <button class="btn btn-block bt-login" type="submit" id="submit_btn" data-loading-text="Signing Up....">Sign Up</button>
            </form>
            <div class="form-footer">
                <div class="row">
                    <div class="col-xs-6 col-sm-6 col-md-6">
                        <i class="fa fa-lock"></i>
                        <a href="forget_password.php"> Forgot password? </a>

                    </div>

                    <div class="col-xs-6 col-sm-6 col-md-6">
                        <i class="fa fa-check"></i>
                        <a href="index.php"> Sign In </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- /container -->


    <script src="js/jquery.validate.min.js"></script>
    <script src="js/register.js"></script>
  </body>
</html>

register.js

$(document).ready(function(){

    $("#register-form").validate({
        submitHandler : function(form) {
            $('#submit_btn').attr('disabled','disabled');
            $('#submit_btn').button('loading');
            form.submit();
        },
        rules : {
            name : {
                required : true,
                name: true,
                remote: {
                    url: "check-name.php",
                    type: "post",
                    data: {
                      name: function() {
                        return $( "#name" ).val();
                      }
                    }
                  }
            },
            email : {
                required : true,
                email: true,
                remote: {
                    url: "check-email.php",
                    type: "post",
                    data: {
                        email: function() {
                            return $( "#email" ).val();
                        }
                    }
                }
            },
            password : {
                required : true
            },
            confirm_password : {
                required : true,
                equalTo: "#password"
            }
        },
        messages : {
            name : {
                required : "Please enter name",
                remote : "Diaplay name already exists"
            },
            email : {
                required : "Please enter email",
                remote : "Email already exists"
            },
            password : {
                required : "Please enter password"
            },
            confirm_password : {
                required : "Please enter confirm password",
                equalTo: "Password and confirm password doesn't match"
            }
        },
        errorPlacement : function(error, element) {
            $(element).closest('div').find('.help-block').html(error.html());
        },
        highlight : function(element) {
            $(element).closest('div').removeClass('has-success').addClass('has-error');
        },
        unhighlight: function(element, errorClass, validClass) {
             $(element).closest('div').removeClass('has-error').addClass('has-success');
             $(element).closest('div').find('.help-block').html('');
        }
    });


});

检查-name.php

<?php
require_once 'config.php';
$db = new Cl_DBclass();
if( isset( $_POST['name'] ) && !empty($_POST['name'])){
    $name = $_POST['name'];
    $query = " SELECT count(name) cnt FROM users where name = '$name' ";
    $result = mysqli_query($db->con, $query);
    $data = mysqli_fetch_assoc($result);
    if($data['cnt'] > 0){
        echo 'false';
    }else{
        echo 'true';
    }
    exit;
}
?>

修改

检查-email.php

<?php
require_once 'config.php';
$db = new Cl_DBclass();

if( isset( $_POST['password'] ) && !empty($_POST['password'])){
    $password =md5( trim( $_POST['password'] ) );
    $email = $_POST['email'];

    if( !empty( $email) && !empty($password) ){
        $query = " SELECT count(email) cnt FROM users where password = '$password' and email = '$email' ";
        $result = mysqli_query($db->con, $query);
        $data = mysqli_fetch_assoc($result);
        if($data['cnt'] == 1){
            echo 'true';
        }else{
            echo 'false';
        }
    }else{
        echo 'false';
    }
    exit;
}

if( isset( $_POST['email'] ) && !empty($_POST['email'])){
    $email = $_POST['email'];
    $query = " SELECT count(email) cnt FROM users where email = '$email' ";
    $result = mysqli_query($db->con, $query);
    $data = mysqli_fetch_assoc($result);
    if($data['cnt'] > 0){
        echo 'false';
    }else{
        echo 'true';
    }
    exit;
}
?>

0 个答案:

没有答案