Using ajax in Drupal 6 for cheking field without username_check. module or ajax.module

Relia's picture

Hello I'm dummy in Drupal!

I found a module which implement the function in AJAX that i want, but it only works in Drupal 5.X. I Prefer use it than username_check.module

What should i change in it, for working good???

this module contains 2 archives:

demo_user.module:

function demo_user_form_alter($form_id, &$form) {
drupal_add_js($module_path ."/demo_user.js");
if ($form_id == 'user_register') {
$username_verify_result = (div) class="reg_form_username" id="username_verify" style="display:none;")
(img src="'.base_path().drupal_get_path('module','demo_user').'/progress.gif" /)
Validating your username...
(/div);
$username_status = '(div class="reg_form_username" id="username_status" style="display:none;")(/div)';
$form['name']['#suffix'] = $username_verify_result.$username_status;
$form['name']['#attributes'] = array('onblur' => 'usernameCheck(this)'); //added the call to our JS function
}
}

/**
* Implemetation of hook_menu()
*/
function demo_user_menu($may_cache){
$items = array();
if (!$may_cache) {
$items[] = array(
'path' => 'user/usernamecheck',
'title' => 'username check',
'callback' => 'demo_user_username_check',
'callback arguments' => array($_GET['username']),
'access' => true,
'type' => MENU_CALLBACK,
);
}
return $items;
}

/**
* ajax user name check.
*
* Checks for the availability of a given username.
* @author Domenic Santangelo
* @param string $username The desired username
* @return string HTML-ified result of the check.
*/
function demo_user_username_check($username) {
$result = db_result(db_query("SELECT name FROM {users} WHERE name = '%s'", $username));
if($result) {
$icon = "error.png";
$msg = " It looks like this username is already in use. Do you want to ". l('retrieve your password?','/user/password');
}
else {
$icon = "ok.png";
$msg = " The username is avaliable.";
}

//If we are working in our local site, a sleep effect could be necessary
sleep(3);
echo "{$msg}";
}

and demo_user.js

function usernameCheck(obj) {
//Spinner shows while the ajax call does its thing
$('#username_status').hide();
$('#username_verify').show();
$('#username_verify').fadeIn('slow');

//Callback function that inserts the result of the username check in the div we set up in Step 1
function domCallback(msg) {
$('#username_verify').hide();
$('#username_status').html(msg);
$('#username_status').fadeIn('slow');
}

//Actually perform the call
$.ajax({
type: "GET",
url: '/user/usernamecheck',
data: "username="+obj.value,
success: function(msg){
domCallback(msg);
}
});
}

Ajax.module

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week