Is a nice solution, that could be used.
http://www.shawngo.com/gafyd/index.html
- A jQuery script senses when first input field is completed and user moves to next input field
- Then javascript calls check.php to validate first input field and give a message.
- The message: Unavailable / Available (if username is okay to register)
- The check.php in this case uses a CSV database. But may as well use MYSQL or whatever database for check.
The HTML form with the javascript:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#usernameLoading').hide();
$('#username').blur(function(){
$('#usernameLoading').show();
$.post("check.php", {
username: $('#username').val()
}, function(response){
$('#usernameResult').fadeOut();
setTimeout("finishAjax('usernameResult', '"+escape(response)+"')", 400);
});
return false;
});
});
function finishAjax(id, response) {
$('#usernameLoading').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
} //finishAjax
</script>
</head>
<body>
<fieldset><legend>Registration Form</legend>
<form action="index.html" method="post">
<p><label for="username">Username:</label> <input type="text" name="username" id="username" />
<span id="usernameLoading"><img src="indicator.gif" alt="Ajax Indicator" /></span>
<span id="usernameResult"></span></p>
<p><label for="password">Password:</label> <input type="password" name="password" id="password"/></p>
<p><input type="submit" name="submit" value="Sign Up!" /></p>
</form>
</fieldset>
check.php
<?php
$username = $_POST['username']; // get the username
$username = trim(htmlentities($username)); // strip some crap out of it
$file = '/home/js4hire/public_html/gafyd/data.csv'; // Here's the file. Notice the full path.
echo check_username($file,$username); // call the check_username function and echo the results.
function check_username($file_in,$username){
$username=strtolower($username);
$file = file($file_in);
foreach ($file as $line_num => $line) {
$line = explode(',',$line);
$user = trim(str_replace('"','',$line[0]));
if($username == strtolower($user)){
return '<span style="color:#f00">Username Unavailable</span>';
}
}
return '<span style="color:#0c0">Username Available</span>';
}
?>
CSV....... data.csv
"username","first_name","last_name","password"
"john","John","Doe","123456"
"martin","Martin","Petrov","111111"