The form always gets submitted. I want to submit it only if data=="available" , i.e ...the php file returned available.
function PostData() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
throw new Error("Ajax is not supported by this browser");
}
var username = document.getElementById("username_input").value;
xhr.open('POST', 'header/onsubmit_check.php');
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("username=" + username);
xhr.onreadystatechange = function () {
var data=xhr.responseText.trim();
document.getElementById('onsubmit_feedback').innerHTML = data;
}
if (data=='Available') {
return true;
} else {
return false;
}
}