How do I change which database table my form data goes into?

How do I change which database table my form data goes into?

by Jason Burrows -
Number of replies: 3
I am trying to create a simple form that has a couple of fields. I am sure I am missing something. I am new to moodle forms. I need my data to go into a table called state_info but have no idea how to get it there. Any help would be greatly appreciated.

Form Code:
<?php // $Id: signature_form.php,v 1 2009/02/07 Exp $

require_once($CFG->libdir.'/formslib.php');
require_once($CFG->dirroot.'/user/profile/lib.php');

class signature_form extends moodleform {
function definition() {
global $USER, $CFG;

$mform =& $this->_form;

//Get Digital Signature for Name

$mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="25"');
$mform->setType('name', PARAM_NOTAGS);
$mform->addRule('name', get_string('missingname'), 'required', null, 'server');

//Get Last Four

$mform->addElement('text', 'lastfour', get_string('lastfour'), 'maxlength="4" size="25"');
$mform->setType('lastfour', PARAM_INT);
$mform->addRule('lastfour', get_string('missinglastfour'), 'required', null, 'server');

//User ID
$mform->addElement('hidden', 'userid', '<?php echo ($USER->id);?>');

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

}

function validation($data) {
global $CFG;
$errors = parent::validation($data);

return $errors;


}}

?>

Here is the other part of the form code:
<?php // $Id: signature.php,v 1 2009/02/07 Exp $
require_once('../config.php');
require_once('signature_form.php');

//HTTPS is potentially required in this page
httpsrequired();

$mform_signature = new signature_form();//name of the form you defined in file above.

if ($mform_signature->is_cancelled()) {
redirect($CFG->httpswwwroot.'/login/index.php');

} else if ($user = $mform_signature->get_data()) {

}

$signature = get_string('signature');
if (empty($CFG->langmenu)) {
$langmenu = '';
} else {
$currlang = current_language();
$langs = get_list_of_languages();
$langmenu = popup_form ("$CFG->wwwroot/course/signature.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
}

$navlinks = array();
$navlinks[] = array('name' => $signature, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header($signature, $signature, $navigation,$mform_signature->focus(), "", true, "<div class=\"langmenu\">$langmenu</div>");

$mform_signature->display();
print_footer();
?>

Average of ratings:Useful (1)
In reply to Jason Burrows

Re: How do I change which database table my form data goes into?

by Frank Ralf -
Hi Jason,

You might enrol in the Moodle Developer Course (http://dev.moodle.org/course/view.php?id=2). Unit 7 of the course gives detailed instruction on creating a form and storing its data in the database.

And there's also "A Step-by-step Guide To Creating Blocks" (http://docs.moodle.org/en/Development:Blocks).

Another starting point is the documentation on formslib.php (http://docs.moodle.org/en/Development:lib/formslib.php).

hth
Frank
In reply to Frank Ralf

Re: How do I change which database table my form data goes into?

by Jason Burrows -
Thanks for the information Frank. I have managed to build a block(installs with tables etc.) but when I try to submit my data it errors out. I get to this point:

For now comment out the redirect and add the following above it

 print_object($fromform);

My Output Shows up

stdClass Object
(
 [MAX_FILE_SIZE] => 10485760
 [courseid] => 2
 [userid] => 2
 [name] => Test User
 [lastfour] => 1234
 [submitbutton] => Submit
)

But nothing is being put into my table. It's as if their is no insert
function occuring. Here is the updated code:

<?php
require_once('../../config.php');
global $CFG, $USER;

require_once('signature_form.php');
$courseid = required_param('courseid',PARAM_INT);

if (! $course = get_record('course', 'id', $courseid) ) {
error(get_string('invalidcourse', 'block_signature'). $courseid);
}

require_login($course);
$signature = new signature_form();

$site = get_site();
print_header(strip_tags($site->fullname), $site->fullname,
 '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'">'.$course->shortname.
 '</a> ->'.get_string('formtitle', 'block_signature'), '',
 '<meta name="description" content="'. s(strip_tags($site->summary)) .'">',
 true, '', '');

if ($signature->is_cancelled()) {
 // cancelled forms redirect to course main page
 redirect("$CFG->wwwroot/course/view.php?id=$id");
} else if ($formresult = $signature->get_data()) {

 if(!insert_record('block_signature',$formresult)){
 error(get_string('inserterror' , 'block_signature'));
}
// we need to add code to appropriately act on and store the submitted data



// redirect("$CFG->wwwroot/course/view.php? id=$courseid");
} else {
 //form didn't validate or this is the first display
 $site = get_site();
 print_header(strip_tags($site->fullname), $site->fullname,
 '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$courseid.'">'.$course->shortname.
 '</a> ->'.get_string('formtitle', 'block_signature'), '',
 '<meta name="description" content="'. s(strip_tags($site->summary)) .'">',
 true, '', '');
}
$toform['courseid'] = $courseid;
$signature->set_data($toform);
$signature->display();
print_footer();

?>


In reply to Jason Burrows

Re: How do I change which database table my form data goes into?

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers
You want something like this after the call to your forms get_data() method:

} else if ($formresult = $signature->get_data()) {

$record = new object;
$record->property = $formresult->property;
$record->another = $formresult->another;

 if(!insert_record('block_signature', $record)){
 error(get_string('inserterror' , 'block_signature'));
}
// we need to add code to appropriately act on and store the submitted data

// redirect("$CFG->wwwroot/course/view.php?id=$courseid");
}