Having one submit button for a function necessitates the running of two ajax functions when the submit button is clicked for validation purposes.
<div class="form-group btn-group">
<input type="button" class="btn btn-link" value="Back" onclick="history.back()">
<input type="button" class="btn btn-primary" class="btn btn-link" value="View results" onclick="validateAndSubmit();">
</div>
async function validateAndSubmit() {
$('.alert-danger').hide();
$('.alert-text ul').text("");
var hasError = false;
<cfif form.output_type eq "cl2stats">
$('.alert-danger').hide().find('ul').empty();
var monthYear1 = $("#date1").val();
var date1 = monthYear1.slice(0, 3) + "01/" + monthYear1.slice(3, 7);
const monthYear2 = $("#date2").val(),
splitted = monthYear2.split('/'),
month = splitted[0],
year = splitted[1],
date2 = `${month}/${new Date(year, month, 0).getDate()}/${year}`;
await makeGetRequest({
url: "url?method=validateDateRange",
data: {date1: date1, date2: date2}
})
.done(function (response) {
if (response == "") {
document.getElementById("EIMEF_WATER_UTILITY_STATS").submit();
} else {
$('.alert-danger').show().find('ul').html(response);
hasError = true;
}
$(window).scrollTop(0);
});
</cfif>
if (hasError == false) {
$.ajax({
type: "POST",
url: "url?method=regStatsExceedancesFilter2",
dataType: "json",
data: ({
formString: formData
}),
success: function (response) {
if (response == "success") {
$('#EIMEF_WATER_UTILITY_STATS').trigger("submit");
} else {
$('.alert-danger').show();
$('.alert-danger .alert-text ul').append(response);
$(window).scrollTop(0);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus + '\n' + "Error: " + errorThrown);
}
});
}
}
In case the first ajax call returns an error, it's vital that the form remains on the page instead of proceeding to the next page as it currently does.