這裏代碼。通常表單類被放置到一個單獨的文件中,這提高了可讀性。
<?php
include(__DIR__.'/config.php'); // Or '/../config.php' or '/../../config.php', you'll figure it out.
require_once($CFG->libdir.'/formslib.php');
class mysuperform extends moodleform {
public function definition() {
global $DB;
$mform = $this->_form;
$options = $DB->get_records_menu('tl_mastercourse', array(), '', "lms_course_id,course_name"); // Consider adding sorting here!
$mform->addElement('select', 'id', '', $options); // Third argument is the label, it was empty in your example.
$this->add_action_buttons(false);
}
}
$PAGE->set_url(new moodle_url('/yourpageurl.php'));
// Call require_login(), require_capability() and other access validation here.
$PAGE->set_context(context_system::instance()); // Only needed if it is not inside a course.
$form = new mysuperform();
if ($form->is_cancelled()) {
// Do stuff, redirect.
} else if ($data = $form->get_data()) {
// Form was submitted, do stuff, redirect.
echo "<pre>";
print_r($data);
exit;
}
echo $OUTPUT->header();
$form->display();
echo $OUTPUT->footer();