2

Can anyone help me check if a username is in my database using ajax and code igniter? I can't use the form_validation method as I have modal windows which interfere with the checking.

Currently my controller looks like:

 function filename_exists(){
        $username = $this->input->post('username');
        $data['exists'] = $this->User_model->filename_exists($username);
    }

My Model:

 function filename_exists($username)
 {

     $this->db->select('*'); 
     $this->db->from('users');
     $this->db->where('username', $username);
     $query = $this->db->get();
     if ($query->num_rows() == 0) {
         return true;
     } else {
         return false;
     }
 }

and my ajax post:

function check_if_exists() {

     <?php $username = $this->input->post('username');
    ?>
     var username = '<?php echo $username ?>';
    var DataString=$("#form1").serialize();
     $.ajax({
     url: "<?php echo base_url(); ?>index.php/Files/filename_exists/",
     type: "post",
     data: DataString + '&username=' + username,
     success: function(response) {


            if (response == true) {
                $('#msg').html('<span style="color: green;">'+msg+"</span>");

            }
             else {

                $('#msg').html('<span style="color:red;">Value does not exist</span>');
            }


         }
     });


}

UPDATE

 <form name = "form1" id = "form1" method ="post"> <!--action="<?php echo base_url()."index.php/Admin/create_user"; ?>"-->
    <?php echo validation_errors(); ?>
    <label for="userID" class = "labelForm">User ID:</label>
    <input type="text" id="userID" name="userID" class = "input2">
    <label for="first_name" class = "labelForm">First Name:</label>
    <input type="text" id="first_name" name="first_name" class = "input2">
    <label for="last_name" class = "labelForm">Last Name:</label>
    <input type="text" id="last_name" name="last_name" class = "input2">
    <label for="username" class = "labelForm">Username:</label>
    <input type="text" id="username" name="username" class = "input2" onblur="check_if_exists();">
    <label for="password" class = "labelForm">Password:</label>
    <input type="password" id="password" name="password" class = "input2" onblur="checkPasswords();">
    <label for="passconf" class = "labelForm">Password:</label>
    <input type="password" id="passconf" name="passconf" class = "input2" onblur="checkPasswords();">
    <label for="email" class = "labelForm">Email:</label>
    <input type="text" id="email" name="email" class = "input2">
  <button type="button"  id = "new_user_submit">Add New User</button>
9
  • change $query->num_rows() == 0 as $query->num_rows() > 0 and test Commented Jun 23, 2016 at 14:34
  • print_r($username) in controller and check data is coming Commented Jun 23, 2016 at 14:35
  • @Abdulla When i try to print the username nothing comes up , but if i console.log(DataString) in the success part of the ajax post it tells me what I have entered? Commented Jun 23, 2016 at 15:25
  • @RejoanulAlam I changed it and no luck unfortunately! :( Commented Jun 23, 2016 at 15:25
  • show your form codes Commented Jun 23, 2016 at 15:26

3 Answers 3

3

Try this

In Ajax

function check_if_exists() {

var username = $("#username").val();

$.ajax(
    {
        type:"post",
        url: "<?php echo base_url(); ?>index.php/files/filename_exists",
        data:{ username:username},
        success:function(response)
        {
            if (response == true) 
            {
                $('#msg').html('<span style="color: green;">'+msg+"</span>");
            }
            else 
            {
                $('#msg').html('<span style="color:red;">Value does not exist</span>');
            }  
        }
    });
}

In Controller

function filename_exists()
{
    $username = $this->input->post('username');
    $exists = $this->User_model->filename_exists($username);

    $count = count($exists);
    // echo $count 

    if (empty($count)) {
        return true;
    } else {
        return false;
    }
}

In Model

function filename_exists($username)
{
    $this->db->select('*'); 
    $this->db->from('users');
    $this->db->where('username', $username);
    $query = $this->db->get();
    $result = $query->result_array();
    return $result
}
Sign up to request clarification or add additional context in comments.

7 Comments

Unfortunately its still not working @Abdulla .. when I console.log(username) I can see the init , but when I write console.log(response) it comes up blank
I've actually found when I echo($count) in the controller it appears to work ( as in have it just before the if statement). This seems like strange behaviour though as when I take it out it seems to return false regardless of the username existing or not.. Is this okay to use like this? @abdulla really appreciate the help as well!
Why query the db for all the users data? Here would be a better query that will take less time. `function filename_exists($username) { $this->db->where('username',$username); return $this->db->count_all_results('users'); }
@AbdullaNilam Sir I get blank response in alert what should i do?
In controller instead of return add echo. Ex: return true should be echo true.
|
2

If you are just trying to see if the user already exists, there is no reason to query with the get() function. just do the count_all_results() it will return a number if the user is found, 0 if not.

function filename_exists($username) {
  $this->db->where('username', $username);
  return $this->db->count_all_results('users'); 
}

All this will do is return a number greater than zero if the username exists in your db.

Comments

0

Just another approach with same result

MODEL

function filename_exists($username) {
   $this->db->select()->from('users')->where('username', $username);
   $query = $this->db->get();

   return $query->first_row('array'); // returns first row if has record in db
}

CONTROLLER

function filename_exists() {
   $username = $this->input->post('username');
   $user = $this->user_modal->filename_exists($username);

   return !empty($user); // not empty returns true else false
}

AJAX

Inside the check_if_exists function.

$.post('<?php echo base_url(); ?>index.php/files/filename_exists', { username: $('#username').val() }, function(response) {
    var html = '<span style="color:red;">Value does not exist</span>';
    if (response == true) {
       html = '<span style="color: green;">' + msg + '</span>';
    } 
    $('#msg').html(html);
});

Comments

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.