1

在我的自定义插件中,我只使用了三个下拉菜单和一个文本框。当我提交表单并validation($data)调用方法时,我只会得到下拉状态的值以及文本框的值。

不返回其他两个下拉列表的值。我不确定我错过了什么。

这是我的代码:

if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
}
require_once($CFG->libdir.'/formslib.php');

class ohio_addconfiguration_form extends moodleform {

// Define the form
function definition() {

    $id = optional_param('id', 0, PARAM_INT);

    $countries = array();
    $states = array();
    $counties = array();
    $cities = array();

    $mform =& $this->_form;

    // Creating hidden variable id
    $mform->addElement('hidden', 'id');
    $mform->setType('id', PARAM_INT);

    // Creating header "Configuration"
    $mform->addElement('header', 'configuration', get_string('ohio', 'local_ohio'));

    /* Listing States */
    $states_result = $this->get_states("", "1", "id, state_name", "state_name ASC");    
    if($states_result) {
        foreach($states_result as $key=>$state){
        $states[$state->id] =  $state->state_name;
        }           
    }       
    $states= count($states)?array(''=>get_string('select_state', 'local_ohio').'...') + $states :array(''=>get_string('select_state', 'local_ohio').'...');
    $mform->addElement('select', 'state_id', get_string('select_state', 'local_ohio'), $states);
    $mform->addRule('state_id', get_string('required'), 'required', null, 'client');
    $mform->setType('state_id', PARAM_INT);

    /* Listing Counties */
    $counties= array(''=>get_string('select_county', 'local_ohio').'...');
    $mform->addElement('select', 'county_id', get_string('select_county', 'local_ohio'), $counties);
    $mform->addRule('county_id', get_string('required'), 'required', null, 'client');
    $mform->setType('county_id', PARAM_INT);

    /* Listing Cities */
    $cities= array(''=>get_string('select_city', 'local_ohio').'...');
    $mform->addElement('select', 'city_id', get_string('select_city', 'local_ohio'), $cities);
    $mform->addRule('city_id', get_string('required'), 'required', null, 'client');
    $mform->setType('city_id', PARAM_INT);

    // Creating text box for School
    $mform->addElement('text', 'school_name', get_string('school_name', 'local_ohio'), 'size="25"');
    $mform->setType('school_name', PARAM_TEXT);
    $mform->addRule('school_name', get_string('required'), 'required', null, 'client');
    $mform->addRule('school_name', get_string('maximumchars', '', 100), 'maxlength', 100, 'client');

    $this->add_action_buttons();
}

function validation($data) {
    global $DB;
    echo "<pre>";
    print_r($data);
    exit;
}
}
4

1 回答 1

0

我不确定您到底在寻找什么,表单验证或数据检索,但我假设您对数据检索感兴趣,并且您上面提供的代码是用“filename_form.php”编写的。

validation() 方法用于验证服务器端字段中输入的数据,而不是获取字段的值。

要获取字段的值,您需要创建另一个名为“filename.php”的文件,并在其中包含“filename_form.php”以显示表单。您可以参考这里使用 Formslib。

于 2013-03-29T10:00:59.710 回答