I'm having some struggles with form validation. I dont have a problem sending the request and receiving the response, but I am having a problem making it either submit or dont submit based on the outcome.
What I am trying to do, is for the user to clicks submit, which fires off the javascript request to ajax_info.php. If that file decides everything is AOK then there will be an empty response, in which case the function should return true and the submit should continue. If there is a response string, then notify the user via alert, and return false so the submit doesnt process.
The only way the alert is happening is if I return false at the end of the javascript function. But if I do that, then the submit never happens. If I put it anywhere else, I dont get the alert (as though the response string is empty, even if it is not), and the submit proceeds regardless of the response.
I hope I am making sense. Thanks.
Code:
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var user = document.getElementById('username2').value;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
return false;
}
else
{
return true;
}
}
xmlhttp.open("GET","ajax_info.php?name=" + user + "&eml=fakemail@gmail.com&ref=filler",true);
xmlhttp.send();
}
</script>
</head>
<body>
<form action='fbauthorize.php' method='post' name='fbform' onsubmit="return loadXMLDoc();">
<center><big><big><b>Please Enter An Account Name:</big><br>
<input type='text' name='username' id='username'><br><br>
<input type='submit' value='Create Account'></form>
</body>
</html>