There are many ways to deal with this issue but for a start why don't you look into autocomplete field
https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#autocomplete
With regards to the change of form this might help:
class myform extends moodleform { protected function definition() { $mform = $this->_form; $mform->addElement('select', 'myselect', 'select', [1 => 'first', 2 => 'second', 3 => 'third']); $mform->setType('myselect', PARAM_INT); $this->add_action_buttons(); } public function definition_after_data() { $mform = $this->_form; if ($this->is_submitted()) { $data = $this->get_data(); if ($data and !empty($data->myselect)) { $mform->addElement('header', 'aaa', 'bbbb'); } } } }
We check if the value of select is something other than 0 and if it is a random element is added.