如何在moodle中创建自定义表单?
我想在moodle中创建一个自定义表单,并将表单数据存储在数据库表中。 我一直在研究moodle表单库,但它对我来说太复杂了如何在moodle中创建自定义表单?,moodle,Moodle,我想在moodle中创建一个自定义表单,并将表单数据存储在数据库表中。 我一直在研究moodle表单库,但它对我来说太复杂了 对于如何在moodle中创建自定义表单并将表单数据存储在数据库中的任何帮助、指导、教程、参考指南或电子书,我们将不胜感激。您有几种方法可以做到这一点。清洁剂是使用表单API() 顺便说一下,您可以使用页面API()在本地插件中使用PHP轻松创建自己的表单 下面是一个简单的例子: <?php require_once('../../config.php'); glo
对于如何在moodle中创建自定义表单并将表单数据存储在数据库中的任何帮助、指导、教程、参考指南或电子书,我们将不胜感激。您有几种方法可以做到这一点。清洁剂是使用表单API() 顺便说一下,您可以使用页面API()在本地插件中使用PHP轻松创建自己的表单 下面是一个简单的例子:
<?php
require_once('../../config.php');
global $CFG, $PAGE;
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_title('Form name');
$PAGE->set_heading('Form name');
$PAGE->set_url($CFG->wwwroot.'/local/yourform/index.php');
echo $OUTPUT->header();
?>
<form method="post" action="post.php">
... Your form code goes here
</form>
<?php
... Your PHP data handling code
echo $OUTPUT->footer();
?>
然后,通过在Moodle根URL的末尾添加
local/yourform/index.php
来访问表单。您有几种方法。清洁剂是使用表单API()
顺便说一下,您可以使用页面API()在本地插件中使用PHP轻松创建自己的表单
下面是一个简单的例子:
<?php
require_once('../../config.php');
global $CFG, $PAGE;
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_title('Form name');
$PAGE->set_heading('Form name');
$PAGE->set_url($CFG->wwwroot.'/local/yourform/index.php');
echo $OUTPUT->header();
?>
<form method="post" action="post.php">
... Your form code goes here
</form>
<?php
... Your PHP data handling code
echo $OUTPUT->footer();
?>
然后,通过在Moodle根URL的末尾添加
local/yourform/index.php
来访问表单。最好使用表单API。这将处理输入验证、表单预填充等
有关详细信息,请参阅:
亮点
经过测试和优化,可在主要屏幕阅读器Dragon和JAWS上使用。
无表布局。
使用必需的参数、可选参数和会话密钥安全地处理表单数据。
支持客户端验证
将Moodle帮助按钮添加到表单的工具。
使用文件API支持文件存储库
支持许多定制的moodle特定和非特定表单元素。
重复元素的加法。
提前组中表单元素的添加
用法
要在moodle中创建表单,必须创建扩展moodleform类的类,并重写包含表单元素的定义
//moodleform is defined in formslib.php
require_once("$CFG->libdir/formslib.php");
class simplehtml_form extends moodleform {
//Add elements to form
public function definition() {
global $CFG;
$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
...
}
//Custom validation should be added here
function validation($data, $files) {
return array();
}
}
然后在页面上实例化表单(在本例中为simplehtml_表单)
//include simplehtml_form.php
require_once('PATH_TO/simplehtml_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
} else if ($fromform = $mform->get_data()) {
//In this case you process validated data. $mform->get_data() returns data posted in form.
} 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($toform);
//displays the form
$mform->display();
}
最好使用表单API。这将处理输入验证、表单预填充等 有关详细信息,请参阅: 亮点 经过测试和优化,可在主要屏幕阅读器Dragon和JAWS上使用。 无表布局。 使用必需的参数、可选参数和会话密钥安全地处理表单数据。 支持客户端验证 将Moodle帮助按钮添加到表单的工具。 使用文件API支持文件存储库 支持许多定制的moodle特定和非特定表单元素。 重复元素的加法。 提前组中表单元素的添加 用法 要在moodle中创建表单,必须创建扩展moodleform类的类,并重写包含表单元素的定义
//moodleform is defined in formslib.php
require_once("$CFG->libdir/formslib.php");
class simplehtml_form extends moodleform {
//Add elements to form
public function definition() {
global $CFG;
$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
...
}
//Custom validation should be added here
function validation($data, $files) {
return array();
}
}
然后在页面上实例化表单(在本例中为simplehtml_表单)
//include simplehtml_form.php
require_once('PATH_TO/simplehtml_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
} else if ($fromform = $mform->get_data()) {
//In this case you process validated data. $mform->get_data() returns data posted in form.
} 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($toform);
//displays the form
$mform->display();
}
有一个为Moodle创建表单的插件,可以让您轻松创建用于注册、反馈、调查和联系的表单。我们可以根据自己的需要进行定制。我们可以使用短代码将表单嵌入Moodle上的任何位置
有一个为Moodle创建表单的插件,可以让您轻松创建用于注册、反馈、调查和联系的表单。我们可以根据自己的需要进行定制。我们可以使用短代码将表单嵌入Moodle上的任何位置