moodle form elements auto posting back, getting value

moodle form elements auto posting back, getting value

by tim st.clair -
Number of replies: 1
Picture of Plugin developers

I'm working on a settings page for my Block. What's the correct, moodley way that I can use a moodle form element (e.g. select) to automatically post back, without validation, and then be able to pick up the value of that form element? Say I have a drop down with 5 elements, and I not only want to capture the value of that dropdown, but also change the display of the next element in the form based on what is selected in the drop down. The course edit form does this with its format dropdown, but it also works somewhat differently to how a block settings form is built so the comparison isn't completely analogous.

$contexts = array('system' => 'System', 'category' => 'Category', 'course' => 'Course', 'group' => 'Group', 'user' => 'User');
$attribs = array('onchange' => 'document.querySelector("#id_updatecontext").click()');
$mform->addElement('select', 'context', 'select context', $contexts, $attribs);
$mform->setDefault('context', 'System');
$mform->registerNoSubmitButton('updatecontext');
$mform->addElement('submit', 'updatecontext', 'update context'); // get_string('updatecontext','block_doclib')
$PAGE->requires->js_init_call('document.querySelector("#id_updatecontext").style.display="none";', null, true);

When the 'context' select postback occurs, I'd like to then perform some kind of switch statement in php to redraw the rest of the form.

(switch postback_value) - when "system", draw a set of form element; when "category" draw a different set of form elements, etc.

Do I just use a $_POST variable? I haven't seen that used elsewhere in the same way in Moodle.

Average of ratings: -