nanobash
nanobash

Reputation: 5500

ajax checking username onblur

here is the case guys, I'm trying to check username on onblur event with help of ajax , which is checking username availability in mysql database.

here is ajax script =>

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 working successfully and .php file too which script is below =>

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

everything is working just fine , but here is the problem , i want to connect somehow this files , I mean that I want to know in .js file when username is matching in database and when not , because I want to do more action in .js file , but I can not set "flag" (variable which will help me for that). Any ideas ? thanks :)))

In more details , .js file is in registration.php file , and how you can see guys .js file is invoking with ajax AjaxUsernameEmail.php file, so I want to do somehow to know when username is matching and when not , because I want in registration.php file to do more actions (notifications) during matching

Upvotes: 0

Views: 2358

Answers (2)

Paul
Paul

Reputation: 9022

For ajax request you must not return the value but print or echo it. Try

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

Evaluationg response:

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;

  }
}

Upvotes: 1

Roberto
Roberto

Reputation: 1944

The code could be a bit more like so:

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

Also, you should be worried about escaping user input, and may want to look into jQuery for your ajax stuff.

The checking on the client side, should go something like this:

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;

    }
}

Upvotes: 1

Related Questions