0

这是案例伙计们,我试图在 ajax 的帮助下检查 onblur 事件上的用户名,它正在检查 mysql 数据库中的用户名可用性。

这是ajax脚本=>

document.getElementById("r_username").onblur = function(){
        var http = false;
        var error = document.getElementById("error_username");
        var numLetter = /^[a-zA-Z-0-9]+$/;
        if (this.value==""){
            error.innerHTML = "Empty Field !!!";
            error.style.display = "inline";
        } else {
            if (this.value.match(numLetter)){
                if (window.XMLHttpRequest){
                    http = new XMLHttpRequest();
                } else {
                    http = new ActiveXObject("Microsoft.XMLHTTP");
                }
                if (http){
                    http.open("POST","./config/AjaxUsernameEmail.php",true);
                    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    http.onreadystatechange = function(){
                        if (http.readyState==4 && http.status==200){

                        }
                    };
                    http.send("r_username=" + document.getElementById("r_username").value);
                }
                error.innerHTML = "";
                error.style.display = "none";
            } else {
                error.innerHTML = "Invalid Number !!!";
                error.style.display = "inline";
            }
        }
    };

ajax 工作成功,.php 文件也在下面=>

class Checking{
private $con,$query,$flag;
public function __construct($con,$query){
    $this->con   = $con;
    $this->query = $query;
}
public function func(){
    if (mysqli_connect_errno()==0){
        if ($result = mysqli_query($this->con,$this->query)){
            if ($data = mysqli_fetch_assoc($result)){
                return $this->flag = true;
            } else {
                return $this->flag = false;
            }
        }
    }
}
}

if (isset($_POST['r_username'])){
    $check = new Checking($connection,"SELECT username FROM users WHERE username='" . $_POST['r_username'] . "'");

} else {
    header("Location: http://" . $mysql->host . "/index.php");
}

一切正常,但问题是,我想以某种方式连接这些文件,我的意思是我想知道 .js 文件中何时用户名在数据库中匹配,何时不匹配,因为我想在其中执行更多操作。 js 文件,但我不能设置“标志”(这将帮助我的变量)。有任何想法吗 ?谢谢 :)))

更详细地说,.js 文件在registration.php 文件中,你怎么能看到家伙.js 文件是用ajax AjaxUsernameEmail.php 文件调用的,所以我想以某种方式知道用户名何时匹配,何时不匹配,因为我希望在 registration.php 文件中在匹配期间执行更多操作(通知)

4

2 回答 2

1

对于 ajax 请求,您必须不是return该值,而是printecho。尝试

if ($data = mysqli_fetch_assoc($result)){
   echo $this->flag = true; exit;
} else {
   echo $this->flag = false; exit;
}

评价反应:

if ( http.readyState == 4 && http.status == 200 ) {
  switch ( http.responseText ) {
    case 1: //user name taken, diplay error message
    break;
    case 0: //user name available, no action required
    break;

  }
}
于 2012-06-04T19:12:49.690 回答
1

代码可能更像这样:

$return = 'fail';

class Checking {

    public function __construct($con, $query)
    {
        $this->con = $con;
        $this->query = $query;
        self::func()
    }

    public function func()
    {
        $result = 'ok';
        if (mysqli_connect_errno()==0){
            if ($result = mysqli_query($this->con,$this->query)){
                $result = mysqli_num_rows($result) > 0? 'user_exists' : 'user_doesnt_exist';
            }
        }
        return $result;
    }

}

if( $_POST['r_username'] ){
    $desired = mysqli_real_escape_string($_POST['r_username']);
    $return = new Checking($connection,"SELECT username FROM users WHERE username='$desired'");
}
echo $return;

此外,您应该担心转义用户输入,并且可能希望查看jQuery以获取您的 ajax 内容。

客户端的检查应该是这样的:

if (http.readyState==4 && http.status==200){
    switch (http.responseText){
        case 'fail':
            //the username was not provided
        break;
        case 'user_exists':
            //the username already exists
        break;
        case 'user_doesnt_exist':
            //the username was not found on the database, continue
        break;

    }
}
于 2012-06-04T19:20:17.980 回答