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>
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:
-
Rakeshkaswan70
-
Reporter
-
Reporter
-
Mark S
-
instatutorial insta
-
instatutorial insta
-
Mark S
-
instatutorial insta
-
http://www.facebook.com/profile.php?id=1016809609 James Barcellano
-
instatutorial insta
-
Categories
-
Articles
-
Calendar
May 2012 M T W T F S S « Aug 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 -
Meta






