<?php
// this is normally where you should include a connection to you database
// to check user input against what is stored
if(isset($_POST["username"])) {
$username = $_POST['username'];
if ($username == "Joe Schmoe") {
echo $username.' is taken';
exit();
} else {
echo $username.' is available';
exit();
}
}
?>
<?php
// this is also where you should include a connection to you database
// to check user input against what is stored
if(isset($_POST["submitIt"])) {
$username = $_POST['submitIt'];
if ($username == "Joe Schmoe") {
echo 'Username is taken';
exit();
} else {
echo 'Success!!';
exit();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<script>
function ajaxObj(meth, url) {
var x = new XMLHttpRequest();
x.open(meth, url, true);
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
}
</script>
<script>
function username_availability() {
var un_value = document.getElementById("username_value").value;
var un_error = document.getElementById("username_error");
var un_status = document.getElementById("username_status");
if (un_value != "") {
var ajax = ajaxObj("POST", "<?php echo $_SERVER['PHP_SELF']; ?>");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
un_status.innerHTML = ajax.responseText;
}
}
ajax.send("username="+un_value);
}
}
function submitIt() {
var un_value = document.getElementById("username_value").value;
var un_status = document.getElementById("username_status");
var sb_button = document.getElementById("submit_button");
var r_message = document.getElementById("result_message");
var un_form = document.getElementById("username_form");
if (un_value == "") {
un_ststus.innerHTML = "Form data is missing";
} else {
un_status.innerHTML = 'processing...';
var ajax = ajaxObj("POST", "<?php echo $_SERVER['PHP_SELF']; ?>");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == "Username is taken") {
un_status.innerHTML = ajax.responseText;
} else {
window.scrollTo(0,0);
r_message.innerHTML = 'Username is yours!!!';
un_status.innerHTML = ajax.responseText;
}
}
}
ajax.send("submitIt=" + un_value);
}
}
</script>
</head>
<body>
<form name="username_form" id="username_form" onsubmit="return false;">
<div>Username: </div>
<input id="username_value" type="text" onkeyup="username_availability()" autofocus>
<button id="submit_button" onclick="submitIt()">Submit</button>
<div id="result_message">Submit Username</div>
<div id="username_status"></div>
</form>
</body>
</html>