2

I have a registration form which is using jQuery validator as:

$('reg_form').validate({
  ignore:"",
  rules : {
    user_name : {
      required :1,
      minlength :8,
      remote :"check_user_name.php"
    },
    password : {
      required :1,
      minlength :8,
    },
    firstname : {
      required :1,
    },
    lastname : {
      required :1,
    },
    email : {
      required :1,
    },
  }
});

It worked pretty well but the problem is remote. It never detects if the username is already in the DB.

The code in remote is as:

$uname = $_GET["user_name"];

$UserObj = new User();
echo $UserObj->checkUniqueUser($uname);

checkUniqueUser() will check if there is an user with the same name it will return false else true.

I got the URL from the request watch

http://project.localhost/check_user_name.php?user_name=admin

I hot linked the URL and it says false, since the admin is already there in the DB and if I change to something else it shows true (of course its not there in DB)

Can someone please help me on this ? Hope I was able to make it clear what I have done so far and what issue I am facing.

This is my function which I have in the User class.

public function checkUniqueUser($username){
      $return = true ;
      global $db ;
      $db->query("select iduser from user where 
      user_name = '".$this->cleanInput($username)."'");
      if($db->numRows() > 0 ){
        $return = false ;
      }
      return $return ;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I think your problem is in the `User` class method `checkUniqueUser`. Could you paste the code of that method? We won't be able to solve this if you don't. If you paste a valid user and it throws false, and with an invalid user throws true, then the problem is that the validations are incorrect, or are swapped. – taxicala Jan 23 '14 at 15:41

1 Answers1

0

Since you want an output, you need to echo true or false from your function, not return. Technically, there's nowhere to return.

See PHP return and PHP echo.

public function checkUniqueUser($username){
      $return = true ;
      global $db ;
      $db->query("select iduser from user where 
      user_name = '".$this->cleanInput($username)."'");
      if($db->numRows() > 0 ){
        $return = false ;
      }
      echo $return ;  // echo (output) true or false
}
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Cool it worked !! I was banging my head for a while :) –  Jan 23 '14 at 16:03
  • Also read through other posts I may need to use echo json_encode(); I will try them out . –  Jan 23 '14 at 16:04
  • @user3228222, yes, the output must be valid JSON, however, AFAIK, I don't think it matters for boolean values. The jQuery Validator also takes a string which would become the error message and this must be JSON. (You must have a certain amount of rep to vote.) – Sparky Jan 23 '14 at 16:07
  • @user3228222, you could always come back after reaching 15. – Sparky Jan 23 '14 at 16:10