title: Check if username exists in database with codeigniter and ajax
body: <p>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.</p>
  
  <p>Currently my controller looks like:</p>
  
  <pre><code> function filename_exists(){
          $username = $this-&gt;input-&gt;post('username');
          $data['exists'] = $this-&gt;User_model-&gt;filename_exists($username);
      }
  </code></pre>
  
  <p>My Model:</p>
  
  <pre><code> function filename_exists($username)
   {
  
       $this-&gt;db-&gt;select('*'); 
       $this-&gt;db-&gt;from('users');
       $this-&gt;db-&gt;where('username', $username);
       $query = $this-&gt;db-&gt;get();
       if ($query-&gt;num_rows() == 0) {
           return true;
       } else {
           return false;
       }
   }
  </code></pre>
  
  <p>and my ajax post:</p>
  
  <pre><code>function check_if_exists() {
  
       &lt;?php $username = $this-&gt;input-&gt;post('username');
      ?&gt;
       var username = '&lt;?php echo $username ?&gt;';
      var DataString=$("#form1").serialize();
       $.ajax({
       url: "&lt;?php echo base_url(); ?&gt;index.php/Files/filename_exists/",
       type: "post",
       data: DataString + '&amp;username=' + username,
       success: function(response) {
  
  
              if (response == true) {
                  $('#msg').html('&lt;span style="color: green;"&gt;'+msg+"&lt;/span&gt;");
  
              }
               else {
  
                  $('#msg').html('&lt;span style="color:red;"&gt;Value does not exist&lt;/span&gt;');
              }
  
  
           }
       });
  
  
  }
  </code></pre>
  
  <p>UPDATE</p>
  
  <pre><code> &lt;form name = "form1" id = "form1" method ="post"&gt; &lt;!--action="&lt;?php echo base_url()."index.php/Admin/create_user"; ?&gt;"--&gt;
      &lt;?php echo validation_errors(); ?&gt;
      &lt;label for="userID" class = "labelForm"&gt;User ID:&lt;/label&gt;
      &lt;input type="text" id="userID" name="userID" class = "input2"&gt;
      &lt;label for="first_name" class = "labelForm"&gt;First Name:&lt;/label&gt;
      &lt;input type="text" id="first_name" name="first_name" class = "input2"&gt;
      &lt;label for="last_name" class = "labelForm"&gt;Last Name:&lt;/label&gt;
      &lt;input type="text" id="last_name" name="last_name" class = "input2"&gt;
      &lt;label for="username" class = "labelForm"&gt;Username:&lt;/label&gt;
      &lt;input type="text" id="username" name="username" class = "input2" onblur="check_if_exists();"&gt;
      &lt;label for="password" class = "labelForm"&gt;Password:&lt;/label&gt;
      &lt;input type="password" id="password" name="password" class = "input2" onblur="checkPasswords();"&gt;
      &lt;label for="passconf" class = "labelForm"&gt;Password:&lt;/label&gt;
      &lt;input type="password" id="passconf" name="passconf" class = "input2" onblur="checkPasswords();"&gt;
      &lt;label for="email" class = "labelForm"&gt;Email:&lt;/label&gt;
      &lt;input type="text" id="email" name="email" class = "input2"&gt;
    &lt;button type="button"  id = "new_user_submit"&gt;Add New User&lt;/button&gt;
  </code></pre>