I'm really new to Moodle and im not sure if im posting this in the right place. I want to create just a simple form, display it in the activity and get the action buttons to work. So here's my code:
simple_form.php
<?php
require_once("$CFG->libdir/formslib.php");
class simplehtml_form extends moodleform {
//Add elements to form
function definition() {
global $CFG;
$mform =& $this->_form; // Don't forget the underscore!
$mform->addElement('header', 'tieudecapnhat', 'Tiêu đề cập nhật');
$mform->addElement('text', 'email', 'Thư điện tử:'); // Add elements to your form
$mform->setType('email', PARAM_NOTAGS); //Set type of element
$mform->setDefault('email', 'Please enter email'); //Default value
// $sql = "select * from mdl_donvi";
// $rs = $DB->get_records_sql($sql);
$FORUM_TYPES=array('1' => 'một','2' => 'hai');
$mform->addElement('select', 'donvi', get_string('selectdonvi', 'facebookintergration'), $FORUM_TYPES);
$this->add_action_buttons();
}
//Custom validation should be added here
function validation($data) {
$returnarray = array();
$returnarray['email']=get_string('required');
$returnarray['donvi']=get_string('required');
return $returnarray;
}
}
And this is how i instantiate the form in view.php
//include simplehtml_form.php
require_once('simpleform.php');
//Instantiate simplehtml_form
$mform = new simplehtml_form();
//Form processing and displaying is done here
if ($mform->is_cancelled()) {
/*
* Link that should redirect to the main page of the course.
*/
die('cancelled');
//Handle form cancel operation, if cancel button is present on form
} else if ($fromform = $mform->get_data()) {
die('validated');
//In this case you process validated data. $mform->get_data() returns data posted in form.
} else {
// this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
// or on the first display of the form.
$toform='invalid@email.com';
//Set default data (if any)
$mform->set_data($toform);
//displays the form
$mform->display();
}
No matter what i put in the handle cancel and validate operation section, everytime i hit the "Save changes" or "Cancel" button, seems like it always tries to redirect me back to the View.php, and display the error:
"error/You must specify a course_module ID or an instance ID"
I know it is asking for a course module ID but it's redirecting itself and i don't know where to put the course module ID.
I'm not really good at PHP too. Any help would be appreciated. Sorry for my bad english.