Here is a basic Username availability check using AJAX
First make sure you have the prototype and scriptaculous libraries and place them in the webroot/js folders.
Initialize it using the following command. I did this in the /views/layout/default.ctp
if(isset($javascript)):
echo $javascript->link('prototype');
echo $javascript->link('scriptaculous');
endif;
Here is the Users Controller
————– Users_controller.php———————
<?php
class UsersController extends AppController {
var $name = 'Users';
// make sure you call the Ajax helper and the RequestHandler component
var $helpers = array('Html', 'Form', 'Javascript', 'Ajax' );
var $components = array('RequestHandler');
function Register(){
// the regular code to add a user
}
function CheckUnique() {
if(!empty($this->data['User']['username'])) {
if ($this->User->findByUsername($this->data['User']['username']))
{
$this->set('error_msg','This Username is already taken');
} else{
$this->set('error_msg', 'This Username is Available');
}
}
}
The register View will need to have an id associated with the textbox, and the Ajax call w
———————- register.ctp————————-
echo $form->input(‘username’,array(‘id’=>’username’)); ?>
<div id=”error_message” ></div>
<?php
//—– Ajax Call
$options = array(‘url’=>’CheckUnique’,'update’=>’error_message’);
echo $ajax->observeField(‘username’,$options);
?>
Now create a view for the UniqueCheck function.. obviously call it unique_check.ctp
and here is the code very easy just a placeholder to update the message
————————unique_check.ctp————————–
<div id”error_message”>
<?php echo $error_msg; ?>
</div>
This is for CakePHP 1.2, I’d not call it very optimized but it works for me
Hope it helps you too