I have a JSP with the form "deleteCont" and a javascript file with the function "validateDelete". When I click the "Delete contact" button the "validateDelete" function is successfully called and the alert with the "alertmessage" is displayed. However, the "ContactMaint" servlet is still called even though false is being returned. Please can someone tell me where I am going wrong?
<form name="deleteCont"
onsubmit="return validateDelete('${sessionScope.account}','${sessionScope.firstname}', '${sessionScope.lastname}')"
action="ContactMaint" method="post">
<input type="submit" value="Delete contact" name="delContact">
</form>
function validateDelete(account, contactFirstName, contactLastName) {
urlstrg = "http://localhost:8080/SalesPoliticalMapping/JCheckContacts?account=" +
account + "&firstname=" + contactFirstName + "&lastname=" + contactLastName;
$.get(urlstrg, function (result, status) {
if (result.length > 0) {
var alertmessage = "Can't delect contact:";
for (var i = 0; i < result.length; i++) {
alertmessage += result[i];
}
alert(alertmessage);
return false;
}
});
}
I have now amended my code as follows although I'm not entirely sure why:
function validateDelete(account, contactFirstName, contactLastName) {
$.ajaxSetup({async: false});
urlstrg = "http://localhost:8080/SalesPoliticalMapping/JCheckContacts?account=" + account + "&firstname=" + contactFirstName + "&lastname=" + contactLastName;
var valid = true;
var alertmessage = "";
$.get(urlstrg, function(result, status) {
if (result.length > 0) {
valid = false;
alertmessage = "Can't delect contact:";
for (var i = 0; i < result.length; i++) {
alertmessage += result[i];
}
}
});
if (valid===false) {
alert(alertmessage);
return false;}
};
return falseshould be outside $.get() [in the scope ofvalidateDelete].