A new comer to Joomla and wish to add the joomla username availability check.

Just look into the Jsfiddle and it is no longer working for me personally

http://jsfiddle.net/exGpH/2/

I believe i made mistakes below

on Javascript

site_root = '';
and
obj.open("GET",site_root+"username_validate.php?username="+user,true);

// I am using a component and it has the files so I do not know what would be the file root

in PHP File

and here' think

$query = "Select a.username FROM #__users where username = '$user' ";
// Execute the above query using your own script and if it return you the
// result (row) we should return negative, else a success message.

$db =& JFactory::getDBO();

$result = $db->setQuery($query);

$available = mysql_num_rows($result);

Plz suggest!!!

Thanks!!!

One problem you've this is how you're embedding your $user variable to your sql query. Without concentrating on the apparent SQL injection risk you're putting yourself along with this method you then have a syntax error:

$query = "Select a.username FROM #__users where username = '$user' ";

ought to be

$query = 'Select a.username FROM #__users where username = "' **. $user .** '" ';

But you want to do something similar to this rather to avoid from an SQL injection attack.

Another problem is when you're calling your after sales validation function. You haev to user Joomla's component url structure to suggest to some function you've defined inside your controller and pointed to inside your router.php. Your controller function will appear somethin like:

function username_validate() {
    $app = JFactory::getApplication(); // this allows you to 

    $requestedUsername = JRequest::getVar("username", null);

    $query = "Select COUNT(username) FROM #__users where username = ' . $requestedUsername . ' ";

    $db =& JFactory::getDBO();

    $result = $db->setQuery($query);

    $available = mysql_num_rows($result);

    echo = $available > = ? "true" : "false";

    $app->close();
}

and also the Hyperlink to refer to it as will appear something similar to this:

'index.php?option=com_mycomponent&task=username_validate&username=' + user

where *com_mycomponent* may be the title of the component.

So ongoing on, this is a quite simple illustration of how you can validate your username area quickly: Username Validation. Let's imagine to dedicate yourself you you would need to substitute the static array to have an AJAX call that returns a listing of usernames but this really is not so clever or secure. A much better approach would be to fire your user input off and away to be looked into and also to return an answer as you've attempted in your soul example.

Making use of your approach you JS will appear something similar to this:

$(document).ready(function() {

    $("#username").keyup(function() {
        var user = $(this).val();

        $.ajax({
            url: 'index.php?option=com_mycomponent&task=username_validate&username=' + user,
            dataType: 'text',
            success:function(responseText, statusText) {

                if($(this).val().length == 0) { 
                    $("#validationTxt").css("display", "none"); 
                    return false; 
                }

                if(responseText == "false"){
                    $("#validationTxt").text("Username Already Taken");
                    $("#validationTxt").css({'color' : 'red', 'display' : 'block'});
                } else {
                    $("#validationTxt").text("Username OK");
                    $("#validationTxt").css({'color' : 'green', 'display' : 'block'});
                }
            }
        });

    });
});

Rather than using .keyup() you can bind the .blur() event which fires an AJAX call off and away to the validation function once the user click or tabs from the username text area. This could reduce the amount of calls make for your after sales. If you would like the feedback because the user types i quickly would highly recommend making an SQL call to retrieve everyone usernames and store them within the Joomla session temporarily so that you can your validation inspections against a range of Strings instead of create a new SQL call each time the consumer types something inside your text area.