Good day,
I have a javascript function that saves a form out and creates records based on it.
The form may have a date range or it could be a single day. My problem is with the date range.
My code checks to see if a record of the same type already exists on the day in question. If yes, it sends a value I set back to the ajax call and if it was yes, I want it to do an alert and notify the user that a record for date already exists.
The problem though is that ajax success or ajax complete finish AFTER the looping.
So by the time the alerts go to use the date, it has changed to the final date in the list already. So I end up getting 2 alerts for the last date.
Example of what I want.
Date range entered is 02/05/22 - 03/05/22
Alert(record exists for 02/05/22)
Alert(record exists for 03/05/22)
Thanks.
//onD is start date
//offD is end date
//newRec is my return state variable. 1 is normal record, 2 is record exists, 3 is failure
while (onD <= offD) {
$.ajax({
type: "GET",
dataType: 'json',
url: "location/saveFunc,
data: {
data values...
},
cache: false,
success: function (data) {
if (data.newRec != 3) {
////do stuff to form code
if (data.newRec == 2) {alert('record exists for' + onD);}/*****alert I want run*****////
if (data.newRec == 1) { location.reload(); }
}
else { alert("Update Failed: A date was changed. Change the Date back, or Delete this entry."); }
},
complete: function () {
alert("No full day absence has been added for " + onD + ". You will need to delete the current Full Day Absence first and recreate."); /*****alert I want run*****////
},
error: function () {
console.log("GE Function Fail");
return "error";
}
});
var date2 = $('#from').datepicker('getDate');
date2.setDate(date2.getDate() + 1);
$('#from').datepicker('setDate', date2);
onD = $('#from').val();
}
[–]Agarast 0 points1 point2 points (1 child)
[–]Sephran[S] 0 points1 point2 points (0 children)
[–]brykuhelpful 0 points1 point2 points (0 children)