0

I use the following code in order to know if some username is already present in my database. I'm using jquery validator, and I configure the script in this way:

$(document).ready(function(){

    // Validate
    // http://bassistance.de/jquery-plugins/jquery-plugin-validation/
    // http://docs.jquery.com/Plugins/Validation/
    // http://docs.jquery.com/Plugins/Validation/validate#toptions

        $('#profile').validate({
        rules: {
          name: {
            minlength: 2,
            required: true
          },
          username: {
            minlength: 2,
            required: true,
            remote: "../check-username.php"
          }
        },
            highlight: function(element) {
                $(element).closest('.control-group').removeClass('success').addClass('error');
            },
            success: function(element) {
                element
                .text('OK!').addClass('valid')
                .closest('.control-group').removeClass('error').addClass('success');
            }
      });

});

where check-username.php is defined as:

<?php 

require_once('connection.php');

$check = "true"; 
$request = trim(strtolower($_REQUEST['username'])); 

mysql_select_db($dbname, $temp);
  $query_username=sprintf("SELECT * FROM users WHERE username = '$request'");

  $resultUsers = mysql_query($query_username, $temp) or die(mysql_error());

   $usernameFound= mysql_num_rows($resultUsers);

   if ($usernameFound> 0) { 
    $check = "false"; 
} 


header("Content-type: application/json; charset=windows-1251");
$result = $_REQUEST['username'].'('.$check.')';
echo $result;

?>

All works good, except the recognition of already existing username. Maybe something is wrong in:

remote: "../check-username.php"

or in the php file itself.

Someone can help me?

EDIT 1:

Trying the @MHR code I obtain this:

enter image description here

In this case "user1" already exists in my database. So this is good, but I don't want that the output is written under my input.

6
  • Debug in firebug and check which url it calling. Commented Apr 25, 2013 at 9:30
  • I get false if username exists Commented Apr 25, 2013 at 9:32
  • except the recognization of yet existing username - what does that mean? Commented Apr 25, 2013 at 9:33
  • @F4r, I was trying to say that my validator can't understand if I type in my form a username already used. Commented Apr 25, 2013 at 9:36
  • I've updated my answer and think that should work. Commented Apr 25, 2013 at 9:59

1 Answer 1

2

http://docs.jquery.com/Plugins/Validation/Methods/remote

The documentation has a shiningly absent example of what the response must be but it hints at JSON:

The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message. For more examples, take a look the marketo demo and the milk demo.

My guess is that you should return "true" if it's valid and "false" if it isn;t:

$check = true; 
$request = trim(strtolower($_REQUEST['username'])); 
mysql_select_db($dbname, $temp);
  $query_username=sprintf("SELECT * FROM users WHERE username = '$request'");
  $resultUsers = mysql_query($query_username, $temp) or die(mysql_error());
   $usernameFound= mysql_num_rows($resultUsers);
   if ($usernameFound> 0) { 
    $check = false; 
} 
header("Content-type: application/json; charset=windows-1251");
if($check){
  $check = "true";
}else{
  $check = "User: ".$request." already exist, please choose another user name.";
}
$result = "\"".$check."\"";
echo $result;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @MHR, I've edited my answer, following your suggestion.
@Joseph82 - you can have this return a string instead of false (i.e. 'That name is already taken') which will be displayed, or you can return nothing and it will use any message you define in your messages option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.