I am currently using an Ajax script to fetch data from my database and insert it into multiple textboxes. Along with posting the data, I also need to perform calculations using these textboxes.
However, upon running the script, I noticed that all calculations are being executed simultaneously. Is there a way to incorporate multiple onSuccess
functions in the script to ensure that the codes run in the correct sequence?
Below is the snippet of my current script:
$(document).on('change', '[id^=vat1]', function getVat12() {
console.log("getVat2 before ajax", jQuery('#vat1').val());
jQuery.ajax({
url: './get/get2.php',
method: 'POST',
data: {'id' : $('#vat1').val()},
success: function(response){
console.log("getPrice after ajax", jQuery('#vat1').val());
jQuery('#percentage1').val(response);
// code 1
var numVal1 = Number(document.getElementById('quantity1').value);
var numVal2 = Number(document.getElementById('price_ex1').value);
var totalValue1 = numVal1 * numVal2
document.getElementById('totalprice1').value = totalValue1.toFixed(2);
//code 2
var numVal3 = Number(document.getElementById('totalprice1').value);
var totalValue2 = numVal3;
document.getElementById('subtotal').value = totalValue2.toFixed(2);
//code 3
var numVal4 = Number(document.getElementById('totalprice1').value);
var numVal5 = Number(document.getElementById('percentage1').value);
var totalValue3 = numVal4 / 100 * numVal5
document.getElementById('vat2').value = totalValue3.toFixed(2);
},
error: function (request, status, error) {
alert(request.responseText);
},
});
});