php - ajax checking username onblur -
here case guys, i'm trying check username on onblur event of ajax , checking username availability in mysql database.
here 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 , .php file script 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 users username='" . $_post['r_username'] . "'"); } else { header("location: http://" . $mysql->host . "/index.php"); } everything working fine , here problem , want connect somehow files , mean want know in .js file when username matching in database , when not , because want more action in .js file , can not set "flag" (variable me that). ideas ? :)))
in more details , .js file in registration.php file , , how can see guys .js file invoking ajax ajaxusernameemail.php file, want somehow know when username matching , when not , because want in registration.php file more actions (notifications) during matching
the code bit more 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 users username='$desired'"); } echo $return; also, should worried escaping user input, , may want jquery ajax stuff.
the checking on client side, should go this:
if (http.readystate==4 && http.status==200){ switch (http.responsetext){ case 'fail': //the username not provided break; case 'user_exists': //the username exists break; case 'user_doesnt_exist': //the username not found on database, continue break; } }
Comments
Post a Comment