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

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题