I need to check same login verification submitted from form with help of AJAX/jQuery
Here is the sources ( jQuery file is not included )
main.html
Code:
<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script language="JavaScript" type="text/javascript">
function verif()
{
var myErr = '';
var f = document.f1reg;
myErr+=" * Fill in all required fields.\n";
$.ajax({
type: "POST",
url: "checkusername.php",
data: "username="+ f.username.value,
success: function(msg){
if (msg == "1"){
myErr+=' * User with such login already exists.\n';
//alert(myErr); Label 1
}
}
});
//alert(myErr); Label 2
if (myErr!='') {
alert('The following information is incorrect:\t\t\t\t\t\n\n'+myErr); // Label 3
}
return (myErr=='');
}
</script>
<form action="action.php" method="post" name="f1reg" id="f1reg" onsubmit="return verif();">
Login: <input type="text" id="username" name="username" value="" tabindex="1" />
<input type="submit" name="submit" value="Submit" />
</div>
</form>
checkusername.php just prints "1"
The problem is the following:
On Label 1, myErr adds an error message to the list
On Label 2, myErr looses the latest message and just shows " * Fill in all required fields.\n";
On Label 3, the same situation as in Label 2
But if I remove Label 2 comment, and alert any message in that part of code, the script starts to working well.
What is the problem?