douqi3195 2015-11-22 19:37
浏览 64
已采纳

codeigniter中的ajax响应

I have this sign-up form with a simple check an the e-mail already exists in database: jquery

 $(document).ready(function () {
        $("#btnregister").click(function () {
            $.ajax({
                type: 'POST',
                dataType:'json',
                url: '<?php echo base_url().'home/register';?>',
                cache : false,
                data: {
                    first_name: $('input[name="first-name"]').val(),
                    last_name: $('input[name="last-name"]').val(),
                    email: $('input[name="email"]').val(),
                    password: $('input[name="password"]').val()
                },
                success: function(data) {
                    console.log(data);
                    if(data.result !== 1){
                        ohSnap('E-mail déja utilisé.', 'red');
                    }

                    else {
                        ohSnap('Votre compte a été crée avec success.', 'green');
                    }


                }

            });

            });

    });

Controller

public function register(){
        $email = $this->input->post('email') ;
        $check_email = $this->users_model->user_exist($email) ;
        $this->output->set_content_type('application/json') ;
        if(!$check_email){
            $data = array(
                'first_name' => $this->input->post('first_name') ,
                'last_name' => $this->input->post('last_name') ,
                'email' => $this->input->post('email') ,
                'password' => $this->input->post('password')
            ) ;
            $this->users_model->create_user($data);
            $this->output->set_output(json_encode(['result' => 1 ])) ;
            return false;
        }else{
            $this->output->set_output(json_encode(['result' => 0 ])) ;
            return false;
        }

    }

Model

   public function user_exist($email){
        $this->db->select('email,id ') ;
        $this->db->where('email', $email);
        $query = $this->db->get('users');
        if($query->num_rows >= 1)
        {
            return $query->result();
        }else{return false;}
    }

The problem is that the controller always responds with the result "1", even if I register with an email that exists in the database. Is there an issue with the model function or what? Please help. Thank you!

  • 写回答

1条回答 默认 最新

  • dongpeng7744 2015-11-22 19:51
    关注

    $query->num_rows should be a function try this instead

    public function user_exist($email){
        $this->db->select('email,id ') ;
        $this->db->where('email', $email);
        $query = $this->db->get('users');
        if($query->num_rows() >= 1)
        {
            return $query->result();
        }else{return false;}
    }
    

    you should switch your environment to development - after that you should get error messages

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料