How do verify user id without refreshing the page?
I use php and javascript during development. How do I verify a user name existence in a database without refreshing the page?(like username entry during registering in expert exchange)
However this will make request on each keypress so you should be carefull on using it. It LOOKS nicer but may slow down your server on many concurent requests.
onBlur will not be so... LIVE as onKeyUp, but it won't mess up with the server so badly, but it will only check for user existance once your field has lost focus (i.e.: click on another field, click ouside the username field, etc...)
If you do NOT want to use ajax, you could get the PHP records in a JavaScript array, and then make your JavaScript function compare the records to whatever the user types by using the above described onKeyUp or onBlur but this is a very buggy method and I won't recommend it. SO good luck with Ajax.
Cipilica
ciprian_dimofte
Here's the full code for you:
1) HTML/JavaScript part (just put this in let's say... index.html):
<html>
<head>
<script>
//Declaration of the AJAX Object
function ajaxFunction(){
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
//The function that checks for the user existance that we setup on the event onKeyUp
function checkUser{
//Initiate the AJAX OBJECT
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState == 4){
//Get the response of the AJAX REQUEST in a HTML DIV (layer)
document.getElementById('responce').innerHTML = xmlHttp.responseText;
}
xmlHttp.open("GET", "ws.php?username="+document.getElementById('username').value, true);
xmlHttp.send();
}
}
</script>
</head>
<body>
<form method="POST" action="someAction.php">
Username:<input type="text" name="username" id="username" size="45" onKeyUp="javascript:checkUser();">
<br />
<div id="responce"></div>
<p>
The rest of your form goes here... Including Submit Buttons, etc...
</form>
</body>
</html>
2) NEXT please create your PHP File named: ws.php just as follows that would be contained in the SAME directory as the one you
created index.html in:
<?php
//Just a basic small connection to a MySQL Database where we supposedly keep the users. Make sure you have the credentials
$link = @mysql_connect("localhost","root","THE_DATABASE_PASSWORD");
$db = @mysql_select_db("DATABASE_NAME", $link);
$err = mysql_error();
if($err){
echo "Error connecting to the database";
exit;
}
//Now, query the USERS TABLE (with SOME but very small SQL injection verification):
$sel = mysql_query("SELECT ID FROM users WHERE username = '".mysql_real_escape_string($_GET['username'])."'");
$res = mysql_fetch_array($sel);
if($res['ID']){
echo "<span style='color:red; font-weight:bold;'>This user is taken !</span>";
}else{
echo "<span style='color:green; font-weight:bold;'>This user is AVAILABLE</span>";
}
?>
I am now assuming you know that you need a database, and a MySQL server, and a Users TABLE in that DATABASE, that would
contain a field called ID (BIGINT) and a field called username (VARCHAR).