I have several ajax calls that posts data to the database through webmethods when the submit button is clicked.
When a user clicks the submit button, an alert box pops up asking the user to verify his/her order before submitting to the database.
When everything looks good and the user clicks the submit button, data is successfully posted to the database.
This part is working fine.
The issue is when the user clicks Cancel, data still posts to the database although this time, blank records get posted to the database.
My question is why is data still getting posted when the user clicks cancel?
Clicking the Cancel option should not trigger ajax call to fire.
What am I missing?
Here is relevant code. More code will be provided if requested.
function getAllCreditorData() {
var data = [];
$('tr.data-contact-person8').each(function () {
var creditname = $(this).find('.creditorname01').val();
var creditaddress = $(this).find('.creditoraddress01').val();
var creditincome = $(this).find('.creditorincome01').val();
var alldata = {
'myCreditorname': creditname,
'myCreditoraddress': creditaddress,
'myCreditorincome': creditincome
}
data.push(alldata);
});
console.log(data);
return data;
}
$("#btnSubmit").click(function (event) {
event.preventDefault();
if (confirm('Please review your order before submitting. Click Ok to submit; Cancel to make additional changes')) {
var empComplete = false, sourceComplete = false, spouseComplete = false, dividentComplete = false, reimbursedComplete = false, honorariaComplete = false, giftComplete = false, orgComplete = false, creditorComplete = false;
function checkComplete() {
if (empComplete && sourceComplete && spouseComplete && dividentComplete && reimbursedComplete && honorariaComplete && giftComplete && orgComplete && creditorComplete) {
$("#result").text("Thank you! You have successfully completed this form");
}
}
}
$("#result").text("");
var data = JSON.stringify(getAllEmpData());
console.log(data);
$.ajax({
url: 'disclosures.aspx/SaveEmpData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
async: false,
success: function () {
empComplete = true;
checkComplete();
},
error: function () {
alert("Error while inserting data");
}
});
Thanks for your help
Your ajax call is outside of your if condition.
$("#btnSubmit").click(function (event) {
event.preventDefault();
if (confirm('Please review your order before submitting. Click Ok to submit; Cancel to make additional changes')) {
$.ajax({
url: 'disclosures.aspx/SaveEmpData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
async: false,
success: function () {
empComplete = true;
checkComplete();
},
error: function () {
alert("Error while inserting data");
}
});
var empComplete = false, sourceComplete = false, spouseComplete = false, dividentComplete = false, reimbursedComplete = false, honorariaComplete = false, giftComplete = false, orgComplete = false, creditorComplete = false;
function checkComplete() {
if (empComplete && sourceComplete && spouseComplete && dividentComplete && reimbursedComplete && honorariaComplete && giftComplete && orgComplete && creditorComplete) {
$("#result").text("Thank you! You have successfully completed this form");
}
}
}
$("#result").text("");
var data = JSON.stringify(getAllEmpData());
console.log(data);
You should enclose the $ajax code inside the if statement that asks the user to confirm() whether they want to submit or not.
Your if statement checks for user confirmation. But the code of the ajax call us not inside the if statement parenthesis. It's outside them. So it will run whether the statement is found true or not.
-RH