当我调用函数时,它给了我错误。为什么?

时间:2019-07-23 10:28:22

标签: javascript codeigniter validation

错误正在出现

我在给用户值赋值时没有调用用户名函数

Message: call_user_func_array() expects parameter 1 to be a valid callback, class 'Error' does not have a method 'index'

我的JavaScript是:

email : { required : true, email : true, remote : { url : baseURL + "checkEmailExists", type :"post"} },
        username :{ required : true, remote : { url : baseURL + "checkUsernameExists", type :"post"} },
        password : { required : true },

我的控制器是:

function checkEmailExists()
{
    $id = $this->input->post("id");
    $email = $this->input->post("email");

    if(empty($id)){
        $result1 = $this->user_model->checkEmailExists($email);
    } else {
        $result1 = $this->user_model->checkEmailExists($email, $id);
    }

    if(empty($result1)){ echo("true"); }
    else { echo("false"); }
}

function checkUsernameExists()
{
    $id = $this->input->post("id");
    $username = $this->input->post("username");

    if(empty($id)){
        $result = $this->user_model->checkUsernameExists($username);
    } else {
        $result = $this->user_model->checkUsernameExists($username, $id);
    }

    if(empty($result)){ echo("true"); }
    else { echo("false"); }
}

我的模型是:

function checkEmailExists($email, $id = 0)
{
    $this->db->select("email");
    $this->db->from("admins");
    $this->db->where("email", $email);   
    if($id != 0){
        $this->db->where("id !=", $id);
    }
    $query = $this->db->get();

    return $query->result();
}

function checkUsernameExists($username, $id = '')
{
    $this->db->select("username");
    $this->db->from("admins");
    $this->db->where("username", $username);   
    if($id != ''){
        $this->db->where("id !=", $id);
    }
    $query1 = $this->db->get();

    echo '<pre>';
    print_r($query1);
    die();

    return $query1->result();
}

我的查看部分是:

<div class="col-md-6">
                                <div class="form-group">
                                    <label for="email">Email address</label><span style="color: red">*</span>
                                    <input type="text" class="form-control required email" id="email"  name="email" maxlength="128">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="username">User Name</label><span style="color: red">*</span>
                                    <input type="text" class="form-control required" id="username"  name="username" maxlength="128">
                                </div>
                            </div>

我的电子邮件验证被完美调用了,但是我的用户名验证却没有完美调用。 它显示call_fun .....的错误 这用于支票电子邮件或用户名是否已经退出

1 个答案:

答案 0 :(得分:0)

在下面的代码中,您无需单独的控制器功能即可检查用户名或电子邮件是否已存在。您可以将其合并为一个控制器功能。

它同时适用于注册和编辑个人资料。

控制器:-

public function fieldcheck()
{
    if( $this->input->is_ajax_request() && ( $this->input->post('email') || $this->input->post('username') ) )
    {
        if( $this->input->post('id') )
        {
            $id = $this->encrypt->decode($this->input->post('id'));
        }
        else
        {
            $id = 0;
        }

        if( $this->input->post('email') )
        {
            $field = 'email';
            $fieldvalue = $this->input->post('email');
        }
        if( $this->input->post('username') )
        {
            $field = 'username';
            $fieldvalue = $this->input->post('username');
        }

        if($field)
        {
            //Loading Model File
            $this->load->model('user_model');

            $result = $this->user_model->checkExists($id,$field,$fieldvalue);
            if ($result == 'new')
            {
                echo json_encode( array('result'=>'new') );
                die();
            }
            else
            {
                echo json_encode( array('result'=>'old') );
                die();
            }
        }
    }
    else
    {
        redirect('user', 'refresh');
    }
}

型号:-

//Querying to Check E-mail or Username is already exists
function checkExists($id,$field,$fieldvalue)
{
    switch($field)
    {
        case 'email'        : $dbfield = 'email';break;
        case 'username'     : $dbfield = 'username';break;
    }
    if($id != 0)
    {
        $option = array( 'id !=' => $id, $dbfield => $fieldvalue );
    }
    else
    {
        $option = array( $dbfield => $fieldvalue );
    }

    //echo "<pre>";
    //print_r($option);
    //die();

    $query = $this->db->get_where('admins', $option);
    if ($query->num_rows() > 0)
    {
        return 'old';
    }
    else
    {
        return 'new';
    }
}

查看:-

这里有1条建议。请用于“电子邮件地址输入”字段。

<div class="col-md-6">
   <div class="form-group">
   <label for="email">Email address</label><span style="color: red">*</span>
   <input type="email" class="form-control required email" id="email" name="email" maxlength="128" onchange="checkusername(this.value);">
   </div>
</div>
<div class="col-md-6">
   <div class="form-group">
   <label for="username">User Name</label><span style="color: red">*</span>
   <input type="text" class="form-control required" id="username" name="username" maxlength="128" onchange="checkemail(this.value);">
   </div>
</div>

JavaScript:-

<script type="text/javascript">
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function checkemail(email)
{
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if( trim(email) != '' && filter.test(trim(email)) )
    {
        $.ajax({
            type        : "POST",
            url         :   "<?php echo site_url('user/fieldcheck');?>", //Controller Name / Function Name
            data        :   { <?php if($this->uri->segment(2) == 'edit' ) echo "'id' : ".$user[0]['id'].", "; ?> 'email' : email },
            dataType    : "json",
            cache       : false,
            success     :   function(data){
                            if( data.result == 'old')
                            {
                                alert('E-mail already Taken.');
                                return false;
                            }
                }
        });
    }
    else
    {
        alert('Please enter valid E-mail Id.');
        return false;
    }
}
function checkusername(username)
{
    if( trim(username) != '' )
    {
        $.ajax({
            type        : "POST",
            url         :   "<?php echo site_url('user/fieldcheck');?>", //Controller Name / Function Name
            data        :   { <?php if($this->uri->segment(2) == 'edit' ) echo "'id' : ".$user[0]['id'].", "; ?> 'username' : username },
            dataType    : "json",
            cache       : false,
            success     :   function(data){
                            if( data.result == 'old')
                            {
                                alert('Username already Taken.');
                                return false;
                            }
                }
        });
    }
    else
    {
        alert('Please enter Username.');
        return false;
    }
}
</script>

希望以上代码有帮助。