Question

My controller function is like this

function check_user() {
        $user_name = $this->input->post('user_name');
        $res_user = $this->user_model->is_unique_user($user_name);
}

And I have user ajax code like below

success: function(response){
                                if(response.data == 'exist'){
                                    $("#user_name").after('<p class="help-block">Username alredy exist</p>');
                                }
                                                                    else{
                                                                            $("#user_name").after('<p class="help-block">Username available</p>');
                                } 

And here is my model function for this

function is_unique_user($user_name)
{
    $this->db->select('*');
    $this->db->where("user_name = '$user_name'");
    $result = $this->db->get('tb_user');

    if($result->num_rows())
    {
        return $date = 'exist';
    }
    else
    {
        return $data = 'available';
    }
}

Now the issue is query is working fine. But No error message. I checked using fire-bug console. It shows error that there is no response. What is wrong here?

Était-ce utile?

La solution

please use die to stop code and return response.

function check_user() {
        $user_name = $this->input->post('user_name');
       $res_user = $this->user_model->is_unique_user($user_name);
       die($res_user);
}

And ajax sucess function change response.data to response

success: function(response){
        if(response == 'exist'){
             $("#user_name").after('<p class="help-block">Username alredy exist</p>');
         }else{
             $("#user_name").after('<p class="help-block">Username available</p>');
       } 

Autres conseils

please review this code ,use echo to get response

function check_user() {
        $user_name = $this->input->post('user_name');
       echo   $res_user = $this->user_model->is_unique_user($user_name);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top