Jquery validator weird behaviour with remote

六月ゝ 毕业季﹏ 提交于 2019-12-12 20:14:59

问题


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 ;
}

回答1:


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
}


来源:https://stackoverflow.com/questions/21312687/jquery-validator-weird-behaviour-with-remote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!