简体   繁体   English

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

[英]How to I check if email exist in user table with codeinighter and 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 查看即时通讯将在视图中林不知道,Ajax代码,如果这是正确的

<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. 上面的ajax代码破坏了该页面的所有JavaScript,而且我还在同一页面上进行了另一个ajax调用,因此我不知道是否可以在一个页面上进行2个ajax调用。

Your ajax should be like this: 您的ajax应该是这样的:

    <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);   
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM