Codeigniter ajax检查电子邮件的可用性

时间:2015-07-28 03:29:03

标签: javascript php jquery ajax codeigniter

我一直在尝试使用ajax和jquery脚本检查电子邮件availibity,如下所示,

我的控制员:

$get_result = $this->user->check_email_availablity();      
        if($get_result == FALSE ) {  
            $validate['message'] = '<p>Email is not available.</p>';
        } else  { 
            $validate['message'] = '<p>Email is available.</p>';
        }            
        $this->load->view('user/signup', $validate);

我的模特:

function check_email_availablity()  {
        $email = $this->input->post('u_email');
        $query = $this->db->query('SELECT u_email FROM tbl_users where u_email = "'.$email.'"');      
        if($query->num_rows() === 1) {   
            return FALSE;
        } else {
            return TRUE; 
        }
    }

我的js:

$(document).ready(function() {     
        /// make loader hidden in start     
        $('#Loading').hide();            
        $('#email').blur(function(){   
        var a = $("#email").val();   
        var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;        
        // check if email is valid   
        if(filter.test(a)){                 
         // show loader     
         $('#Loading').show();    
         $.post("<?php echo base_url()?>main/signup", {     
         email: $('#email').val()    
        },
        function(response){                         
         //#emailInfo is a span which will show you message     
         $('#Loading').hide();     
         setTimeout("finishAjax('Loading', '"+escape(response)+"')", 400);    
        });    
        return false;   
       } 
       }); 
       function finishAjax(id, response){   
        $('#'+id).html(unescape(response));   
        $('#'+id).fadeIn(); 
       }
    });

我的观点:

 <?php echo form_input('u_email', set_value('u_email'), 'class="form-control" id="email"'); ?>
                <span id="Loading"><?php echo $message; ?></span>

我的问题是型号总是返回TRUE并显示电子邮件可用&#39;消息,如何查看电子邮件可用性

2 个答案:

答案 0 :(得分:1)

在模型中更改查询行中的if条件:

function check_email_availablity()  {
        $email = $this->input->post('u_email');
        $query = $this->db->query('SELECT u_email FROM tbl_users where u_email = "'.$email.'"');      
        if($query->num_rows() > 0) {   
            return FALSE;
        } else {
            return TRUE; 
        }
    }

在你的控制器中:

       $get_result = $this->user->check_email_availablity();      
        if(!$get_result) //if email already exist in your database
        {  
            $validate['message'] = '<p>Email is not available.</p>';
        } else  { 
            $validate['message'] = '<p>Email is available.</p>';
        }            
        $this->load->view('user/signup', $validate);

答案 1 :(得分:0)