Initially I don't use the cancel
button in my moodle form
. My form looks like:
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');
}
Now I want to use cancel
button in my form so I replace the line
$this->add_action_buttons(false, 'submit');
with
$this->add_action_buttons(true, 'submit');
The cancel
button is displayed successfully but while clicking the cancel
button the form gets submitted
(even if there is no data in form fields).
How can I resolve this? Am I miss anything else??
Please help me...
EDIT
Depending upon Russell England answer. I try like below:
$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();
But still the cancel button
submit the form.
You need to check if the form has been cancelled in the calling php file. eg:
$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.
}