I have a form on my website where in some cases I need to validate a phone number and in other cases I don't have to (based on changes in the input field.
The form is populated with data from an database and if the input "phone_Number" has been changed I need to validate the number when submit is pressed.
If the input "phone_number" hasn't changed the form has other inputs that needs to be validated.
My issue is: If the phone number has changed I need to make 2 calls with ajax instead of 1, and I can't figure out what the best way to do that is. I have read that async: false was deprecated, so I need help how to arrange my code in order for the submit to work.
I have looked at some different jquery when/wait examples, but I do not understand jquery good enough to change the examples results into my question.
$(document).on('submit', '#order_form', function(event){
let form_data = new FormData($('#order_form')[0]);
let phone_number = $('#phone_number').val();
form_data.append('customer_name',$('#customer_name').val());
if($('#old_number').val() != phone_number) {
$.ajax({
url: 'https://api.numlookupapi.com/v1/validate/' + phone_number + '?apikey=api_key',
dataType: 'json',
success: function(json) {
form_data.append('valid',json.valid);
form_data.append('number',json.number);
form_data.append('local_format',json.local_format);
.... some additional "form_data.append" happens here ....
}
});
} else {
form_data.append('local_format',phone_number);
}
$.ajax({
url: "process.php",
method: 'POST',
data: form_data,
contentType: false,
processData: false,
success: function () {
.... do something ....
}
});
});`