// 2) Utility to read from your visible widgets (covers common patterns)
function readPatientTypeUI(){
// Try common selectors; customize if you have a specific element
return (
$('[data-for="patient_type"]').val() ||
$('#patient_type_visible').val() ||
$('select[name="patient_type_visible"]').val() ||
$('.spmedical-appointment-combobox:visible').val() || // fallback if your styled select has this class
'' // last resort
);
}
function readHmoNameUI(){
return (
$('#hmo_name_visible').val() ||
$('input[name="hmo_name_visible"]').val() ||
$('#hmo_name').val() || // if your visible input already has id="hmo_name"
'' // last resort
);
}
// 3) Keep hidden fields in sync when user changes the UI fields
$(document).on('change input', '[data-for="patient_type"], #patient_type_visible, select[name="patient_type_visible"], .spmedical-appointment-combobox', function(){
$('#patient_type_hidden').val(readPatientTypeUI());
});
$(document).on('change input', '#hmo_name_visible, input[name="hmo_name_visible"], #hmo_name', function(){
$('#hmo_name_hidden').val(readHmoNameUI());
});
// 4) Final guard: force-sync on submit
$form.on('submit', function(){
var pt = readPatientTypeUI();
var hmo = readHmoNameUI();
$('#patient_type_hidden').val(pt || $('#patient_type_hidden').val() || '');
$('#hmo_name_hidden').val(hmo || $('#hmo_name_hidden').val() || '');
// Optional: quick client check — block if required but empty
if (!$('#patient_type_hidden').val()){
alert('Please select your patient type.');
return false;
}
return true;
});
// 5) (Nice-to-have) Initial sync on page load
$('#patient_type_hidden').val(readPatientTypeUI());
$('#hmo_name_hidden').val(readHmoNameUI());
});