2

请帮助我,有人知道我哪里出错了,这开始让我烦恼。

我正在使用moodle 2.2和快速表单,它会在提交时保存到数据库中,但随后会返回错误的表单。

mysqli::real_escape_string() 期望参数 1 是字符串,数组在/$root/lib/dml/mysqli_native_moodle_database.php中给出

我还尝试将表单中的文档上传到课程上下文中以进行大规模挣扎。

<?php

require_once("../../config.php");

$courseid=2;      

if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
    error('Site is misconfigured');
}

$context = get_context_instance(CONTEXT_COURSE, $course->id);

require_login($courseid);

/// Otherwise fill and print the form.
$thetitle = 'Edit Vacancy';

$PAGE->set_title($thetitle);
$PAGE->set_heading($thetitle);
$PAGE->set_pagelayout('base');
$PAGE->navbar->add($thetitle);
$PAGE->set_url('/systems/phones/index.php');

require_once('create_form.php');

//Instantiate simplehtml_form 
$mform = new simplehtml_form();

//Form processing and displaying is done here
if ($mform->is_cancelled()) {
//Handle form cancel operation, if cancel button is present on form
redirect('view.php');

} else if ($fromform = $mform->get_data()) {
//In this case you process validated data. $mform->get_data() returns data posted in form.
$toform = new stdClass();
$toform->title = $fromform->title;
$toform->refno = $fromform->refno;
$toform->closedate = $fromform->closedate;
$toform->hours = $fromform->hours;

$options = array('subdirs'=>1, 'maxbytes'=>$CFG->userquota, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);

$toform = file_postupdate_standard_filemanager($toform, 'files', $options, $context, 'user', 'private', 0);

$DB->insert_record('systems_jobs', $toform);

} 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.

//Set default data (if any)

$mform->set_data();
//displays the form
}

echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();
?>

使用我的表格:

<?php

if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
}

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('text', 'refno', 'Post Number:'); // Add elements to your form
$mform->setType('refno', PARAM_NOTAGS);//Set type of element

$mform->addElement('text', 'title', 'Post Title:'); // Add elements to your form
$mform->setType('title', PARAM_NOTAGS);//Set type of element

$mform->addElement('filepicker', 'reference', 'Specification:', null, array('maxbytes' => $CFG->maxbytes, 'accepted_types' => '*'));
if (empty($entry->id)) {
    $entry = new stdClass;
    $entry->id = null;
}

$mform->addElement('date_selector', 'closedate', 'Close Date:', array(
    'startyear' => 2012, 
    'stopyear'  => 2020
));

$mform->addElement('editor', 'hours', 'Info:');
$mform->setType('hours', PARAM_RAW);

$this->add_action_buttons();
}

//Custom validation should be added here
function validation($data, $files) {
    $errors = parent::validation($data, $files);

    $mform = $this->_form;

    $errors = array();

    if ($mform->elementExists('refno')) {
            $refno = trim($data['refno']);
            if ($refno == '') {
                $errors['refno'] = get_string('required');
            }
    }

    if ($mform->elementExists('title')) {
            $title = trim($data['title']);
            if ($title == '') {
                $errors['title'] = get_string('required');
            }
    }

        return $errors;
    }
}
?>
4

1 回答 1

3

我刚刚加入了这个网站,刚刚浏览了关于moodle的帖子,偶然发现了这个帖子。我认为您现在可能已经解决了这个问题,但是对于其他任何人:

当您在任何页面上显示 moodle 表单时,您应该这样做:

if($form->is_cancelled()) {
    //Write the code to handle the event where user clicks the cancel 
    //button on the form
    //This is where (generally) you would use the redirect function
} else if($data = $form->get_data(true)) {
    //This is where you can process the $data you get from the form
} else {
    //This is where you should add the commands that display the page when 
    //displaying the form for first time (when page loads)
    echo $OUTPUT->header();
    echo $OUTPUT->heading();
    //Add other commands
    echo $OUTPUT->footer();
}

干杯桑迪普

于 2013-03-10T12:48:20.713 回答