My implementation:
- I add all the possible options from the database.
1. file motivation_form.php
- I add all the possible options from the database.
- When loading the page using JS, I hide all the "options" elements.
- I get the ID of the elements I need and display only them
$mform->addElement('select', 'napravlenie', 'Планируемое направление подготовки ',$spec, array('onchange' => 'javascript:getValue()'));2. file class-section.js
$prof = $DBDEAN->get_records_sql_menu("SELECT rp.id, concat(rp.name, '(',re.formname,')') FROM mdl_bsu_grad_subdep_speciality ss
INNER JOIN mdl_bsu_ref_profiles rp ON rp.id = ss.profileid
INNER JOIN mdl_bsu_ref_edforms re ON re.id = ss.edformid
GROUP BY rp.id ORDER BY rp.name");
$prof[0] = 'Выберите профиль';
ksort($prof);
$mform->addElement('select', 'profff', 'Планируемая программа подготовки',$prof);
$(document).ready(function() {3. file programma.php
$("#id_profff").children('option').hide();
getValue();
});
function getValue() {
var select = document.getElementById("id_napravlenie");
var sclt = select.value;
$.ajax({
url: "programma.php", //url страницы (script.php)
type: "GET", //метод отправки
dataType: "json",
data: "sclt=" + sclt,
success: function (response) {
var arrprof = response;
$.each(arrprof,function(index,value){
$("#id_profff").children("option[value^=" + value + "]").show();
});
},
error: function (response) { // Данные не отправлены
$('#result_form').html('Ошибка. Данные не отправлены.');
}
});
}
$sql = "SELECT rp.id, rp.id as idprof FROM mdl_bsu_grad_subdep_speciality ss
INNER JOIN mdl_bsu_ref_profiles rp ON rp.id = ss.profileid
where specialityid = {$_GET['sclt']} AND ss.yearid = 21 AND ss.kvalif = 9
GROUP BY rp.id ORDER BY rp.name ";
$data = $DBDEAN->get_records_sql_menu($sql);
echo json_encode($data);