取消按钮在Moodle中提交表单数据

时间:2015-07-03 09:19:50

标签: php moodle cancel-button

最初我没有使用cancel中的moodle form按钮。我的表格如下:

require_once("{$CFG->libdir}/formslib.php");

class quest_form extends moodleform {

    function definition() {
        global $DB;
        $mform =&$this->_form;

        $mform->addElement('header','displayinfo', 'Add Question');

        // add question.
        $mform->addElement('editor', 'title', 'Question');
        $mform->addRule('title', null, 'required', null, 'client');
        $mform->setType('title', PARAM_RAW);
        // add answer.
        $mform->addElement('editor', 'answer', 'Answer');
        $mform->addRule('answer', null, 'required', null, 'client');
        $mform->setType('answer', PARAM_RAW);

        $mform->addElement('hidden', 'blockid');
        $mform->setType('blockid', PARAM_RAW);
        $mform->addElement('hidden', 'courseid');
        $mform->setType('courseid', PARAM_RAW);
        $this->add_action_buttons(false, 'submit');
    }

现在我想在表单中使用cancel按钮,以便替换

$this->add_action_buttons(false, 'submit');

$this->add_action_buttons(true, 'submit');

cancel按钮成功显示,但点击了cancel按钮the form gets submitted(即使表单字段中没有数据)。

我该如何解决这个问题?我还想念别的吗?

请帮帮我......

修改

取决于 Russell England 的答案。我尝试如下:

    $qform = new quest_form ();
    if ($qform ->is_cancelled()){
      redirect("view.php?id={$cid}");
    }else{
      $qform = new pool_form("pool_action.php?id={$cid}&qpid={$questplace->id}&qtype={$qtype}");
    }   
    $qform ->display();

cancel button仍然提交表单。

1 个答案:

答案 0 :(得分:0)

您需要检查表单是否已在调用的php文件中取消。例如:

$form = new quest_form();

if ($form->is_cancelled()) {
    // Display a message or redirect somewhere.
    redirect(new moodle_url('/foldername/cancelquesturl.php'));
} else if ($data = $form->get_data()) {
    // Do something with the data.
}