test1.php
Instead of using a click handler for the button, use the form submit event.
$(document).ready(function () {
$('#username').change(function () {
var userName = $('#username').val();
$.post("getUserName.php", {
userName: userName
}, function (data) {
$("#userNameCheck").html(data);
});
});
$('#addform').submit(function () {
//if the text is `You can use it` allow the form submit else block it
return $("#userNameCheck").html().trim() == 'You can use it';
});
});
Also make sure that you do the same validation in test2.php
because, the client side validation can be side stepped.