我正在从数据库字段创建一个表单,所以我拉出所有记录并循环并在php中的foreach循环中添加表单元素。问题是,当我提交表单时,元素未发布,我得到的唯一回报是提交按钮: -
stdClass Object
(
[submitbutton] => Submit
)
这是我创建元素的方式,这些都在屏幕上正确显示和显示,它只是在我提交时不发布,但如果我没有在foreach循环中使用它们,则元素会发布,但我需要从数据库动态创建它们,任何想法?
foreach($records as $log){
$inc++;
if($log->type == 0){
$mform->addElement('html', '<p>'.$log->leadin.'</p>');
$attributes = array();
$distractors = explode(',', $log->distractors);
$radioarray=array();
$count = 0;
foreach($distractors as $dis){
$count++;
$radioarray[] =& $mform->createElement('radio', 'radio', '', $dis, $count, array());
}
$mform->addGroup($radioarray, 'radioar'.$inc, '', array(' '), false);
}
else if($log->type == 1){
$mform->addElement('html', '<div>'.$log->leadin.'</div>');
$distractors = explode(',', $log->distractors);
$count = 0;
foreach($distractors as $dis){
$count++;
$mform->addElement('checkbox', 'check'.$count, $dis);
}
}}
填写表格代码: -
class build_user_survey extends moodleform{
public function definition() {
global $CFG;
global $DB;
global $OUTPUT;
$mform =& $this->_form;
$errors= array();
$br = "<br />";
$select = '';
$records = $this->_customdata['thequestions'];
$inc = 0;
$attributes=array('rows'=>'10','cols'=>'80');
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'viewpage');
$mform->setType('viewpage', PARAM_INT);
$mform->addElement('hidden', 'pickedsurvey');
$mform->setType('pickedsurvey', PARAM_INT);
$mform->addElement('hidden', 'questiontype');
$mform->setType('questiontype', PARAM_INT);
foreach($records as $log){
$inc++;
if($log->type == 0){
$mform->addElement('html', '<div>'.$log->leadin.'</div>');
$distractors = explode(',', $log->distractors);
$count = 0;
foreach($distractors as $dis){
$count++;
$mform->addElement('radio', 'radio'.$inc, '', $dis, $count, array());
}
}
else if($log->type == 1){
$mform->addElement('html', '<div>'.$log->leadin.'</div>');
$distractors = explode(',', $log->distractors);
$count = 0;
foreach($distractors as $dis){
$count++;
$mform->addElement('checkbox', 'check'.$count, $dis);
}
}
else if($log->type == 2){
echo "<script type='text/javascript'>alert('here');</script>";
$thename = 'answer';
$mform->addElement('textarea', $thename, $log->leadin, $attributes);
}
}
foreach($records as $log){
$mform->addElement('radio', 'radio', '', 'ioh;', 0, array());
}
$mform->addElement('textarea', 'answerWorking', '$log->leadin', $attributes);
$this->add_action_buttons($cancel = true, $submitlabel='Submit');
}
public function validation($data, $files) {
global $DB;
$errors= array();
if($data['id']) {
return true;
}
}
}
答案 0 :(得分:0)
复选框只会在选中后返回一个值。
如果您还想要未选择的值,请使用高级复选框
$mform->addElement('advcheckbox', 'check'.$count, $dis);
http://docs.moodle.org/dev/lib/formslib.php_Form_Definition#advcheckbox
编辑:我刚刚复制了代码并将复选框更改为advcheckbox并且它正常工作:)您还需要在for循环之前放置$ count = 0.
致电代码
$thequestions = array();
$thequestion = new stdClass();
$thequestion->type = 1;
$thequestion->leadin = 'leadin 1';
$thequestion->distractors = 'dis1, dis2, dis3';
$thequestions[] = $thequestion;
$thequestion = new stdClass();
$thequestion->type = 1;
$thequestion->leadin = 'leadin 2';
$thequestion->distractors = 'dis4, dis5, dis6';
$thequestions[] = $thequestion;
$mform = new build_user_survey(null, array('thequestions'=>$thequestions ));
if ($data = $mform->get_data()) {
var_dump($data);
}
$mform->display();
Var转储
object(stdClass)[3270]
public 'id' => int 0
public 'viewpage' => int 0
public 'pickedsurvey' => int 0
public 'questiontype' => int 0
public 'check1' => string '0' (length=1)
public 'check2' => string '0' (length=1)
public 'check3' => string '0' (length=1)
public 'check4' => string '0' (length=1)
public 'check5' => string '0' (length=1)
public 'check6' => string '0' (length=1)
public 'radio' => string '0' (length=1)
public 'answerWorking' => string '' (length=0)
public 'submitbutton' => string 'Submit' (length=6)
修改后的表单代码
class build_user_survey extends moodleform {
public function definition() {
$mform =& $this->_form;
$records = $this->_customdata['thequestions'];
$inc = 0;
$attributes=array('rows'=>'10','cols'=>'80');
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'viewpage');
$mform->setType('viewpage', PARAM_INT);
$mform->addElement('hidden', 'pickedsurvey');
$mform->setType('pickedsurvey', PARAM_INT);
$mform->addElement('hidden', 'questiontype');
$mform->setType('questiontype', PARAM_INT);
$count = 0;
foreach($records as $log){
$inc++;
if($log->type == 0){
$mform->addElement('html', '<div>'.$log->leadin.'</div>');
$distractors = explode(',', $log->distractors);
foreach($distractors as $dis){
$count++;
$mform->addElement('radio', 'radio'.$inc, '', $dis, $count, array());
}
}
else if($log->type == 1){
$mform->addElement('html', '<div>'.$log->leadin.'</div>');
$distractors = explode(',', $log->distractors);
foreach($distractors as $dis){
$count++;
$mform->addElement('advcheckbox', 'check'.$count, $dis);
}
}
else if($log->type == 2){
echo "<script type='text/javascript'>alert('here');</script>";
$thename = 'answer';
$mform->addElement('textarea', $thename, $log->leadin, $attributes);
}
}
foreach($records as $log){
$mform->addElement('radio', 'radio', '', 'ioh;', 0, array());
}
$mform->addElement('textarea', 'answerWorking', '$log->leadin', $attributes);
$this->add_action_buttons(true, 'Submit');
}
}
答案 1 :(得分:0)
解决了!我发现首次加载页面时我在URL中传递的额外参数以及首次创建表单时还需要在提交表单时存在,因此url上的额外参数搜索数据库记录,以及何时我提交了表单,页面被重新加载,所有函数再次被调用并失败,因为缺少参数并且找不到记录。希望这有助于其他人。