1

Direct to the point. I am checking that my entered username exists or not exist in the database. So I have decided to use ajax.

The expected result is like when I enter the username which already exists then the div style get change and error message get appear under it but this is not happing now I don't know why?

I am not familiar with ajax so any syntax missing or any wrong technique you see then you can point it out.

This is my controller Function:

function check_userName_exists()
{
    $user_name = $this->input->post('user_name');
    log_message('debug', 'Posted Username by Ajax for Check it exist or not : '. print_r($user_name, TRUE));

    $exists = $this->student_db->check_user_name($user_name);

    log_message('debug', 'Username Exist or not Response array: '. print_r($exists, TRUE));

    $count = count($exists);
    // echo $count 
    log_message('debug', 'Username Exist or not Response Count: '. print_r($count, TRUE));

    if ($count == 1) {
        $data['unique'] = 1;
        print(json_encode($data));
    } else {
        $data['unique'] = 0;
        print(json_encode($data));
    }
}

This is my model function:

function check_user_name($user_name)
{

    $this->db->select('user_name'); 
    $this->db->from('user');
    $this->db->where('user_name', $user_name);
    $query = $this->db->get();
    $query_last = $this->db->last_query();
    log_message('debug', 'Find UserName is Unique Or Not Query: '. print_r($query_last, TRUE));
    $result = $query->result_array();
    log_message('debug', 'Find UserName is Unique Or Not Result: '. print_r($result, TRUE));
    return $result;
}

This is the Ajax script:

<script type="text/javascript">
function check_if_exists() {

var user_name = $("#user_name").val();
// console.log(user_name);

$.ajax({
  type:'POST',
  url: "<?php echo site_url('student/check_userName_exists'); ?>",
  data:{ 'user_name':user_name},
  success:function(response)
  {

    if (response.unique = 1) 
    {
      // alert(success);
      //console.log(response);
      $("#userNameDiv").addClass("has-warning");
      $("#user_name").addClass("form-control-danger");
      $("#userNameErrorMsg").text("This User Name Is Already Takken. Please Try Something Else.");
    }
    else 
    {


      $("#userNameDiv").removeClass("has-warning");
      $("#user_name").removeClass("form-control-danger");      
      $('#userNameErrorMsg').text('');
    }    
  }
});

};

This is my HTML:

<div class="form-group" id="userNameDiv">                    
                <input type="text" class="form-control form-control-success" name="user_name" required id="user_name" placeholder="Enter Username" onblur="check_if_exists();">
                <small class="text-help" id="userNameErrorMsg"></small>
              </div>

got blank response in console.

3
  • I get the blank response in the consol Commented Mar 19, 2017 at 7:40
  • change return 1 and return 0 in your controller to echo '1' and echo '0'. Controller function in CI does not return the response, it should output it. Commented Mar 19, 2017 at 8:00
  • I am losing hope on this question that i ever get the proper answer. Commented Mar 19, 2017 at 9:05

2 Answers 2

2

Your controller function is always going to return 0. Count returns an integer, you're checking if it's empty... Updated the controller code below to fix that.

Ajax expects a json response not a single value.

In your controller print the return value, don't return it.

if ($count > 0) {
    $data['unique'] = 1;
    print(json_encode($data));
} else {
    $data['unique'] = 0;
    print(json_encode($data));
}

Then in your ajax function

if (response.unique == '1') 
Sign up to request clarification or add additional context in comments.

11 Comments

Sir @RickCalder Sir i have tried your code and every time response get in to the else part of condition even if i entered the existed username.
console log response as the first thing in the success block (before the if statement)
ah, also try removing the quotes around the 1 in the ajax if statement if(response.unique==1) instead?
get the response in the consol sir but the class did not work. Here I add danger class if user name is exist but it didnot show any effect on that div
See edit, your controller code was checking to see if $count was empty. It won't ever be empty, count() returns an integer so if there is no result it will be 0 not empty
|
0

return won't work here, just echo your response instead

if (empty($count)) {
    echo '1';
} else {
    echo '0';
}

also check whatever response from server

success:function(response)
{
   console.log(response); // at beginning  

   // your code

 }

1 Comment

I have edited my question. I just not get the effect on the div while entering a non-unique username.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.