在codeigniter中调用ajax期间的查询执行无效

时间:2014-04-30 06:02:49

标签: jquery ajax codeigniter codeigniter-2

以下代码包含我对函数validate_username_alone()的ajax调用(通过远程)。

$("#register").validate({
    rules: 
    {
        email: 
        { 
            required: true, email: true,
        },
        username_r: 
        {
            required: true, minlength: 4,
            remote:
            {
                url: "http://localhost/karthi/my_site_t/index_page/validate_username_alone",
                type: "post"
            }
        },
        password_r:{required:true,minlength: 6},
        c_password:{required:true,equalTo: "#password_r"}
    },
    messages: 
    {
        username_r: 
        {
            required: "Please specify your name",
            minlength: "Minimum length is 4",
            remote: "Username already taken"
        },
        email: 
        {
            required: "We need your email address to contact you",
            email: "Your email address must be in the format of name@domain.com"
        },
        password_r:
        {
            required:"Enter your password",
            minlength:"Password must be minimum 6 characters"
        }
    }
});

我的validate_username_alone()包含以下代码:

function validate_username_alone()
{
    $username = $this -> input -> post('username_r');
    $query = $this->db->query("SELECT username FROM user WHERE username = '".$username."'");
    if ($query->num_rows() == 1)
    {
        echo json_encode(TRUE);
    }
    else
    {
        echo json_encode(FALSE);
    }
}

现在即使我返回json_encode(FALSE),在两种可能性中,我都没有收到错误:用户名已经被占用。

但是将整个函数内容替换为json_encode(FALSE)会产生预期的错误:已经使用了用户名。

有什么问题?

0 个答案:

没有答案