Reputation: 17
I am new Moodle and working on form data saving. I have created a group of checkbox which will take some input from the users and store it to the database, but unfortunately it is storing only 0/1 in the database. I have no clue where I made the mistake. This my php file where I added the form field-
class custom_signup_form extends moodleform {
//Add elements to form
public function definition() {
global $CFG;
global $DB;
$mform = $this->_form; // Don't forget the underscore!
//$mform->addElement('text', 'email', get_string('email')); // Add elements to your form.
//$mform->setType('email', PARAM_NOTAGS); // Set type of element.
//$mform->setDefault('email', 'Please enter email'); // Default value.
if($this->content !== Null){
return $this->content;
}
$courses = $DB->get_records('course');
$categories = $DB->get_records('course_categories');
$interestedcourse=array();
foreach($courses as $course){
$coursestring = $course->fullname;
$interestedcourse[] = $mform->createElement('advcheckbox', 'interestedcourse[]','', $coursestring, array('group' => 1), array('',$coursestring));
}
$mform->addGroup($interestedcourse, 'interestedcoursegroup', get_string('interestedcourse', 'assignsubmission_metadata'),array('<br>'), false);
$interestedcategory=array();
foreach($categories as $category){
$categorystring = $category->name;
$interestedcategory[] = $mform->createElement('advcheckbox', 'interestedcategory[]','', $categorystring, array('group' => 1), array('',$categorystring));
}
$mform->addGroup($interestedcategory, 'interestedcategorygroup', get_string('interestedcategory', 'assignsubmission_metadata'),array('<br>'), false);
$this->add_action_buttons();
This is the php file for saving form data-
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/local/custom_signup/classes/form/edit.php');
global $DB;
$PAGE->set_url(new moodle_url('/local/custom_signup/signupform.php'));
$PAGE->set_title(get_string('custom_signup', 'local_custom_signup'));
#custom form for signup
$mform = new custom_signup_form();
if ($mform->is_cancelled()) {
// Go back to manage.php page
redirect($CFG->wwwroot . '/local/custom_signup/manage.php', get_string('cancelled_form', 'local_message'));
}
else if ($fromform = $mform->get_data()) {
var_dump($fromform);
die;
}
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->render_from_template('local_custom_signup/signupform', $templatecontext);
echo $OUTPUT->footer();
object returned from the form submission-
object(stdClass)#288 (3) { ["interestedcourse"]=> array(1) { [""]=> string(0) "" } ["interestedcategory"]=> array(1) { [""]=> string(0) "" } ["submitbutton"]=> string(12) "Save changes" }
Upvotes: 0
Views: 269