Codeigniter 中的 Jquery 验证器插件已存在电子邮件无法正常工作

Email already exist with Jquery validator plugin in codeigniter is not working

我在 Codeigniter 中使用 jquery 验证器插件,我想检查电子邮件字段是否已存在给定的电子邮件。我已根据 Jquery 验证器文档中提供的说明完成了所有必要的操作,但它在 Codeigniter 中无法正常工作。有人可以帮我解决这个问题吗?

<script>

    $(document).ready(function () {

        $("#signupFrm").validate({
            rules: {
                customer_contactNumber: {
                    required: true,
                    digits: true,
                    minlength: 10,
                    maxlength:14
                },
                customer_email: {
                    required: true,
                    email: true,
                    remote: {
                        url: "<?php echo base_url(); ?>index.php/account/register_email_exists",
                        type: "post",
                    }
                },
            },
            messages: {
                customer_contactNumber:{
                    required: "Please enter your Contact Number",
                    minlength: "Contact number should not be lesser than 10 digits",
                    maxlength: "Contact number should not be higher than 14 digits",
                    digits: "Please enter numbers Only"
                },
                customer_email:{
                    required: "Please enter your email",
                    email: "Please enter a valid email",
                    remote: "Email already exist"
                },
            }
        });

    });

</script>

注册表单

<div class="form-group has-feedback">
    <input type="email" class="form-control" placeholder="Email" id="customer_email" name="customer_email">
    <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>

控制器

function register_email_exists()
{
    if (array_key_exists('customer_email',$_POST))
    {
        $email=$_POST["customer_email"];

        $this->load->model("Email_auth");

        $this->Email_auth->verifyEmailExist($email);

        if ($this->Email_auth->verifyEmailExist($email) == TRUE)
        {
            echo json_encode("FALSE");
        }
        else
        {
            echo json_encode("TRUE");
        }
    }
}

Email_auth.php

<?php

class Email_auth extends CI_Model {

    function verifyEmailExist($email) 
    {
        $this->db->select('customer_id');

        $this->db->where(array("customer_email" => $email));

        $query=$this->db->get('tbl_customer');

        if ($query->num_rows() > 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
}

此答案假定您的远程 CodeIgniter PHP 函数正在正确查询您的数据库。

  1. 不需要 根本不需要 data 参数,除非你想发送字段之外的信息。默认情况下,该字段的值已经发送。

    删除所有这些:

    data:{
        customer_email: function() { 
            return $("#customer_email").val();
        }
    }
    
  2. 您的布尔值需要用引号引起来。在 "true""false" 的情况下,它在有和没有 json_encode() 的情况下都有效。但是,如果您想发送消息字符串而不是 "false",则 json_encode() 是必需的。

    echo json_encode("true");

    echo "true";

  3. 最后,如果您在 CodeIgniter 中启用了 CSRF 保护,那么您必须在 data 参数中发送隐藏令牌的值。同样,您不会发送 customer_email 字段的值,因为默认情况下已经发送了。 (将下面的 csrftoken 替换为您的令牌的实际 name。)

    data: {
        csrftoken: function() {
            return $('input[name="csrftoken"]').val();
        }
    }