Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:13:01+00:00 2026-06-05T01:13:01+00:00

here is the case guys, I’m trying to check username on onblur event with

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:13:03+00:00Added an answer on June 5, 2026 at 1:13 am

    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;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok here I have a switch case statement which is falling through and trying
Here is the case. User should be able to open MS Word document which
An odd case I'm trying to figure out here. I'm trying to design a
Guys, I'm trying to finish up my homework but I'm having some problems here
hi guys im new here =) i would need your help in a minimal
After an extensive unsuccessful search I hope you guys can help me out here...
First of all. I appreciate the help guys! This is the problem.Trying to set
Here's a puzzle for you guys. I have a multithreaded program in which some
Hey, guys. Newbie to tomcat/apache, java returner here. I'm trying to run th step-by-step
I am hitting a wall and have come here hoping you guys can help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.