When i was working on a project of mine then i was stuck for checking username using ajax (jquery) then i tried a lot and after all i got a way around. In this post i will tell you about that how to check whether a user already exists or not with provided username.

I have wrote complete code below:

Code for your view file.

This code is to be placed in your views file:

  • Html Code
  • 
    <label>Username</label>
    <?php echo $this->Form->input('username',array('label'=>false,
                             'div'=>false,'type'=>'text',
                             'id'=>'name',
                             'class'=>'username',
                             'value'=>'Username'));
    ?>
    <div id="username_feedback"></div>
    
  • Css Code
  • 
    
  • Javascript Code
  • 
    

    Above html + javascript code must be placed in view file;
    But Css code is optional either you put it in view file or place it in any stylesheet;

Code for Controller.

here i suppose that you have controller named users. you just put below code in your users controller file. If you have controller with any other name (not users) then just change above javascript code (replace users with your controller name).

function check_username()
{
    $this->autoRender=false;
if(mb_strlen($username)===0){
        echo "Choose a username";
        }
else if(mb_strlen($username)<5){
        echo "Too Short Username!";
        }
else{
    if($this->RequestHandler->isAjax()) {
     mb_internal_encoding("UTF-8");
     $username=trim($this->params['form']['username']);
     $conditions = array("User.username" =>$username);
     //either you use cake's query method or find method....
     //using find method...
     $query=$this->User->find('first', array('conditions' => $conditions));
     //using query method...
     $query=$this->User->query("SELECT username FROM users
                                WHERE username= 'mysql_real_escape_string($username)'");

       if(mb_strlen($query)!==0){
        echo "Username Taken!";
        }
      else{
        echo "You Entered Correctly!";
        }
      }
    }
}

Final Words

I gave you complete code. Hope you can workout, even if some problem occurs you can contact me at instatutorial@gmail.com
Enjoy jquerying….

Related posts:

Tagged with:
 
  • Rakeshkaswan70

    gud for me

  • Reporter

    it’s working… thanks buddy…

  • Reporter

    it’s completely working thanks for the post….
    one thing more it is more simpler than other’s methods…

  • Mark S

    as i commented somewhere else about this blog post, this is not a good example for other beginners. you do not use cake’s find() method and you do not escape/secure the input. this is dangerous and negligent.
    besides this very important “bug” in your code there are a few other things that are not very good coding:
    you check for !empty($username) after you queried, you use strlen() instead of the correct mb_strlen() as I assume you use utf8 as encoding.

  • instatutorial insta

    thank you mark… i will change some code as per your telling….
    sorry i was not working on this blog from few days…
    anyways can you help me to improve it….

  • instatutorial insta

    thank you for checking my code… i have corrected it now….

  • Mark S

    I think you missunderstood. !empty() is fine. just not AFTER the query, but BEFORE (why hitting the DB if no username was given?).
    also note, that you NEVER use != or == with strlen() or any related method. you always need type secure checks (!== and ===) for those methods. it is documented in the php manual, i think.

  • instatutorial insta

    oh sorry i got you…
    *corrections again…
    thank you sir…

  • http://www.facebook.com/profile.php?id=1016809609 James Barcellano

    You are breaking MVC. You never want to output from your controller. You should setup a view. Also, there is no need to call SQL like that in cakephp. You could simply use the model’s object’s find() method. This is a bad example for CakePHP, as you’re not really utilizing the tools it gives you.

  • instatutorial insta

    james there is no problem in doing this…. if i m not using a view is because i m utilizing $this->autoRender=false; variable… which cake provides us…
    then you are talking about sql query… this is also given by cake developers….
    so that you can use it to write custom queries… and by the way i have wrote both functions query() as well as find() , whichever you want to use you can…