databaseinsertmoodleform-api

Inserting data into the database when 'submit' button pressed in moodle forms


I'm using Moodle installation v 2.3.4, and have created a simple form in the NEWMODULE plugin and using it to enter 2 fields, name and description.

I want to insert the data entered in the database, but the data is not getting inserted. After pressing Submit, moodle looks for modedit.php, which is ofcourse not in the present directory, thus 'page not found' error is displayed.

The code snippets are shown: make_form.php (The forms page):

<?php
require_once('../../config.php');
require_once('mod_form.php');
require_login($course, true);

echo $OUTPUT->header();
$mform = new mod_testform_mod_form();

if ($mform->is_cancelled()) {

}
else if ($fromform = $mform->get_data()) {

//      print_object($fromform);    
    $record = new stdClass();       
    $record->id='';
    $record->name= $fromform->name;
    $record->description= $fromform->desc;
    $DB=insert_record('testform_details', $record, false);
    $mform->display();
}
else {
    $mform->set_data($toform);
    $mform->display();
    print_footer($course);
}
?>

mod_form.php

<?php

defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/course/moodleform_mod.php');

class mod_testform_mod_form extends moodleform_mod {

public function definition() {

    $mform = $this->_form;

    $mform->addElement('header', 'general', get_string('general', 'testform'));
    $mform->addElement('text', 'name', get_string('name', 'testform'));
    if (!empty($CFG->formatstringstriptags)) {
        $mform->setType('name', PARAM_TEXT);
    } else {
        $mform->setType('name', PARAM_CLEAN);
    }
    $mform->addRule('name', null, 'required', null, 'client');
    $mform->addHelpButton('name', 'name', 'testform');

//      $this->add_intro_editor();
    $mform->addElement('editor', 'desc', get_string('description','testform'));
    $mform->setType('desc', PARAM_RAW);
    $mform->addHelpButton('desc', 'description', 'testform');

    $buttonarray=array();
    $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges'));
    $buttonarray[] = &$mform->createElement('reset', 'resetbutton', get_string('reset'));
    $buttonarray[] = &$mform->createElement('cancel');
    $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
    $mform->closeHeaderBefore('buttonar');
}
}

Solution

  • Your file structure and your code is not correct according to activity plugin take a look at

    http://docs.moodle.org/dev/Activity_modules

    follow exact as per the document.

    you are getting error of modedit.php because you are using clase of mooodleform_mod , and you dont need to create this file in your folder its core file wich will automatically open once you follow correct syntax according to mod plugin.

    if you just want to store the data into db then use local plugin its more easy than mod plugin.

    Thanks.