繁体   English   中英

使用PHP CodeIgniter,JQuery,AJAX在数据库中存在用户名验证

[英]Username verification if already exists in the database using PHP CodeIgniter, JQuery, AJAX

我想在用户使用Codeigniter点击提交按钮之前先检查用户名的有效性。 用户名的状态(如果已被使用)将仅显示在旁边。

这是视图中的JS:

<script type="text/javascript" src="../assets/js/jquery.min.js"></script>
<script type="text/javascript"><!--
 $(document).ready(function() {
    $("#username").blur(function() {
        //remove all the class add the messagebox classes and start fading
        $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
        //check the username exists or not from ajax


        $.post("addpatient/check_user_availability",{ username:$(this).val() } ,function(data) {
            if(data=='no') {
                $("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox

                    //add message and change the class of the box and start fading
                    $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
                });

            }
            else {
                $("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox

                    //add message and change the class of the box and start fading
                    $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
                });
            }
        });

    });

});  
</script>


这是html中表单的一部分:

<form method="post" action="addpatient/insert" name="register">
        <table id='addpatient'>
            <tr>    
                <td width=100> Username:
                <td width=150>  <input type='text' id='username' name='username' value="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>" placeholder="5-15 char" pattern=".{5,15}" required>
                <td> <span id="msgbox" style="display:none"></span>
                <td><div id="msgbox"></div>
            </tr>...


这是控制器:

function check_user_availability()
    {
        $username = $this->input->post('username');

        //  Developed by Roshan Bhattarai 
        //  Visit http://roshanbh.com.np for this script and more.
        //  This notice MUST stay intact for legal use


        //this varible contains the array of existing users
        $existing_users=$this->user->get_all_usernames();
        //value got from the get metho
        $user_name= $username;

        //checking weather user exists or not in $existing_users array
        if (in_array($user_name, $existing_users))
        {
            //user name is not availble
            echo "no";
        } 
        else
        {
            //user name is available
            echo "yes";
        }
    }


这是模型中的一个功能:

function get_all_usernames() {
    $this->db->select('username');
    $this->db->from('users');
    $query = $this->db->get();
    return $query->result();
 }



但是,当我运行它时, 它总是说用户名可以注册,即使已经使用了用户名。

另一个问题是, 当用户在“实时”中键入他/她期望的用户名时它不检查用户名的有效性 我的意思是,用户必须在代码检查有效性之前先点击下一个文本字段。

请帮我解决这些问题。 谢谢!

尝试这个:

function check_user_availability()
{
    $username   = $this->input->post('username');
    $result     = $this->user->get_all_usernames( $username );  #send the post variable to the model
    //value got from the get metho
    $user_name= $username;

    if( $result ){
        echo "no";
    }else{
        echo "yes";
    }
}

function get_all_usernames( $username ) {
    $this->db->select('username');
    $this->db->where('username', $username)
    $this->db->from('users', 1);
    $query = $this->db->get();
    if( $query->num_rows() > 0 ){
        return 0;
    }else{
        return 1;
    }
}

脚本中的更改:

$(document).ready(function() {
    $("#username").keyup(function() {
        //remove all the class add the messagebox classes and start fading
        $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
        //check the username exists or not from ajax

        var name    = $(this).val();
        $.ajax({
            url     : "<?=base_url()?>addpatient/check_user_availability",
            data    : { username : name },
            type    : 'POST',
            success : function(data){
                if( data == '0' ){
                    $("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox

                        //add message and change the class of the box and start fading
                        $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
                    });
                }else if( data == '1' ){
                    $("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox

                        //add message and change the class of the box and start fading
                        $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
                    });
                }else{
                    alert("Some error occured! Check Console");
                }
            }
        });
    });
}); 

控制器:

function check_user_availability() {
    $username = $this->input->post('username');

    $this->form_validation->set_rules('username', 'Username', 'required|is_unique[users.username]');

    if ($this->form_validation->run() == FALSE) {
        //user name is not availble
        echo "no";
    } else {
        //user name is available
        echo "yes";
    }
}

用上面的控制器替换控制器中的check_user_availability()。 无需使用get_all_usernames model

查看Form_Validation,set_rules的文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM