based on previously selected value

based on previously selected value

by Denis Sh -
Number of replies: 1

I am trying to create a profile using a moodle form. In the questionnaire, the student first selects the Direction, and then the training profile. The values are taken from the database. <Select> with a profile should be generated based on the selected direction, i.e.

SELECT rp.id, rp.name  FROM mdl_bsu_ref_profiles rp where specialityid = {$_GET['sclt']}

I did not find any standard methods for this implementation.

Try it with jQuery

function getValue() {
var select = document.getElementById("id_napravlenie");
var sclt = select.value;
$.ajax({
url: "programma.php", //url страницы с запросом к БД
type: "GET",
data: "sclt="+sclt,
success: function(response) { //Данные отправлены успешно
$('#id_prof').html(response); // вставляем вернувшиеся данные в select
},
error: function(response) { // Данные не отправлены
$('#result_form').html('Ошибка. Данные не отправлены.');
}
});
}
Everything works in the interface (screen above), but when the form is submitted, the form element

$mform->addElement('select', 'prof', 'Планируемая программа подготовки ',$prof);
"prof" is not present at all in the object, i.e. once we changed the content of select '#id_prof' with jQuery, its data is not written to the DB.
Database entry code:
if ($mform->is_cancelled()) {
redirect('/my/');
} else
if ($datanew = $mform->get_data()) {
if($datanew->id == 0) {
$DB->insert_record('asp_motivation', $datanew);
} else {
$DB->update_record('asp_motivation', $datanew);
}
$redirlink = "motivation.php";
redirect($redirlink, get_string('changessaved'), 0);
}
else {
$data = $DB->get_record_select('asp_motivation', "uid={$USER->id}");
$mform->set_data($data);
$mform->display();
}
I would be grateful if someone can suggest or help. Sorry for the English, I used translate.google

Average of ratings: -
In reply to Denis Sh

Re: based on previously selected value

by Denis Sh -
My implementation:
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

1. file motivation_form.php
$mform->addElement('select', 'napravlenie', 'Планируемое направление подготовки ',$spec, array('onchange' => 'javascript:getValue()'));

$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);
2. file class-section.js
$(document).ready(function() {
$("#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('Ошибка. Данные не отправлены.');
}
});
}
3. file programma.php
$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);