2012-08-01 25 views
2

請幫幫我,有人知道我哪裏出錯了,這開始讓我煩惱。quickforms和moodle

我使用的是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; 
    } 
} 
?> 
+0

您的錯誤沒有行號或堆棧跟蹤。嘗試安裝xdebug,然後發佈它會給你的更具體的錯誤信息。 – 2012-08-07 16:21:37

+0

如上所述,您應該提供確切的錯誤消息。我注意到,插入後沒有重定向:錯誤必須是由於與mform相關的數據,因此如果您仍想顯示此內容,而不是返回索引,則可以嘗試重定向到同一頁面你正在編輯的東西。 – 2013-03-07 09:15:31

回答

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(); 
} 

乾杯 桑迪普