0

New to Joomla and want to add the joomla username availability check.

Just check the Jsfiddle and its not working for me

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

I think 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 I 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!!!

1 Answers1

0

One issue you have here is how you are embedding your $user variable into your sql query. Without focusing on the obvious SQL injection risk you are putting yourself in with this approach you also have a syntax error:

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

should be

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

But you should do something like this instead to prevent from an SQL injection attack.

Another issue is how you are calling your backend validation function. You haev to user Joomla's component url structure to point to a function you have defined in your controller and pointed to in your router.php. Your controller function will look 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 the URL to call it will look something like this:

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

where com_mycomponent is the name of your component.

Ok so continuing on, here is a very simple example of how to validate your username field on the fly: Username Validation. For this example to work for you you would have to substitute the static array for an AJAX call that returns a list of all usernames but this is not very clever or secure. A better approach is to fire your user input off to be checked and to return a response as you have attempted in you example.

Using your approach you JS will look something like 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'});
                }
            }
        });

    });
});

Instead of using .keyup() you could bind the .blur() event which fires an AJAX call off to the validation function when the user click or tabs out of the username text field. This would reduce the number of calls you have to make to your backend. If you want the feedback as the user types then I would strongly recommend making an SQL call to retrieve all of you usernames and store them in the Joomla session temporarily so you can your validation checks against an array of Strings rather then make a new SQL call everytime the user types something in your text field.

Community
  • 1
  • 1
travega
  • 8,284
  • 16
  • 63
  • 91
  • Is this above script is using Mootool ? or JS? Because my joomla have mootool and not JQuery. Plz suggest. – user1169852 Jan 26 '12 at 08:31
  • I find a example for this how can i implement it using Joomla? http://davidwalsh.name/ajax-username-availability-checker-mootools – user1169852 Jan 26 '12 at 09:13
  • Yes it's in jQuery. You can use jQuery with Joomla and having used Mootools and jQuery I much prefer jQuery. You can use both side by side but if you are adamant you dont want to use jQuery then you can convert the syntax to mootools, its very similar frameworks. – travega Jan 26 '12 at 19:06