I have one submit button for a function and for validation purposes, I need to have two ajax functions run when the submit is clicked.
<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); } }); } } If the first ajax call returns an error, I need the form to stay on the page. Currently, if the first call returns an error, it moves to the next page.
var hasError = false; if (hasError == false)totally redundant, sincehasErroris guaranteed to always befalseon the line of code after setting it falsesuccessfunction of the first call.if (response == "success")block.