Ajax Username Check - Using jQuery!

Registration Form

Ajax Indicator


What happens?

The embedded javascript picks up the onblur event of the input field with id username.

It then sends an ajax post request to check.php which checks the csv for the filename and returns the result.

The embedded javascript then updates the span element with id usernameResult.

The Code!

index.html - check source

jQuery - a super javascript utility! check out jQuery

data.csv - CSV file that contains the user information

"username","first_name","last_name","password"
"john","John","Doe","123456"
"martin","Martin","Petrov","111111"

check.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>';
}
?>