Codeigniter - Update email only if the email doesn't exist in the database

前端 未结 3 825
借酒劲吻你
借酒劲吻你 2021-01-17 04:00

I have an update page for my users where they can edit their name, email and other info.

So far, they can edit everything. Including their email. They can enter an e

3条回答
  •  佛祖请我去吃肉
    2021-01-17 04:34

    Your Javascript file:

    $("#form_id").validate({
            rules: {
                email: {
                    required: true,
                    email: true,
                    remote: {
                        type: "post",
                        url: "pathtocontroller/controller.php/checkEmail",
                     }
                }
            },
            messages: {
                email: {
                    required: "Please enter Email!",
                    email: "Please enter valid Email!",
                    remote: "Email already not available!"
                }
            }
    

    Your Controller File:

    function checkEmail() {
            $userArr = $this->input->post();
            $id = $this->session->userdata('id');//if you have stored id within session else pass it within remote function
            if (isset($userArr["email"])) {
                if ($id != '') {
                    $ext_cond = "id !='" . $id . "'";
                }
                echo $this->your_model_name->getUserValidation('your_email_field_name', $userArr['email'], $ext_cond);
                exit;
            }
            exit;
        }
    

    Your Model:

    public function getUserValidation($usertype = '', $value = '', $cond = '') {
    
            if ($usertype != '' && $value != '') {
                $this->db->select($usertype);
                $this->db->from($this->main_table);
                if ($cond != '') {
                    $this->db->where($cond);
                }
    
                if (is_array($usertype)) {
                    foreach ($usertype as $key => $type_value) {
                        $this->db->where($type_value, $value[$key]);
                    }
                } else {
                    $this->db->where($usertype, $value);
                }
    
                $user_data = $this->db->get()->result_array();
    //            echo $this->db->last_query();exit;
                if (is_array($user_data) && count($user_data) > 0) {
                    return "false";
                } else {
                    return "true";
                }
            } else {
                return "false";
            }
        }
    

提交回复
热议问题