douqian2334 2016-09-23 18:50
浏览 16

如何使用codeinighter和ajax检查用户表中是否存在电子邮件

I want users to enter their email click a button and then check to see if the email is already in the database this is what i tried so far with no success

model

public function email_exists($email) {
        $this->db->select('*'); 
        $this->db->from('users');
        $this->db->where('email', $email);
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
}

Controller

 function email_exists(){
          $this->load->model('main_model');
          $email = $this->input->post('email');
          $exists = $this->main_model->email_exists($email);
          $count = count($exists);
          echo $count;  
           if (empty($count)) {
                return true;
          } else {
               return false;
         } 
 }

View Im adding the ajax code in the view im not sure if this is right

<input id="about-email" type="email">
<div id="about-you" >enter</div>
<script>
       var email = $("#about-email").val();
       $('#about-you').click(function() {
        $.ajax({
                    type:"post",
                    url: "<?php echo base_url(); ?>index.php/my_controller_name/email_exists",
                    data:{ email:email},
                    success:function(response)
                    {
                        if (response == true) {
                            $('#msg').html('<span>email exists</span>');
                        } else  {
                            $('#msg').html('<span>Value does not exist</span>');
                        }  
                    }
                });
    });
</script>   

The ajax code above breaks all my javascript for that page I'm also making a another ajax call on the same page so I don't know if it possible to make 2 ajax calls on one page.

  • 写回答

1条回答 默认 最新

  • dongtaigan1594 2016-09-23 20:16
    关注

    Your ajax should be like this:

        <script>
           var email = $("#about-email").val();
           var url = <?= base_url("my_controller_name/email_exists") ?>
           $('#about-you').click(function() {
           $.ajax({
                        type:"post",
                        url: url,
                        dataType: "json",
                        data: {email: email},
                        success:function(response)
                        {
                            if (response) {
                                $('#msg').html('<span>email exists</span>');
                            } else  {
                                $('#msg').html('<span>Value does not exist</span>');
                            }  
                        }
                    });
            return false;
        });
        </script>
    

    And in your controller:

        function email_exists(){
            $email = $this->input->post('email');
            $this->load->model('main_model');
            $exists = $this->main_model->email_exists($email);
            $exists = (count($exists) > 0)? true : false;
            echo json_encode($exists);   
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大